Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions index.js

This file was deleted.

2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
import "./src/command.ts"
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
"name": "node_tutorial",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"bin": {
"note": "./index.js"
"note": "index.ts"
},
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
Expand All @@ -25,5 +24,8 @@
"jest": "^29.6.4",
"open": "^9.1.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"typescript": "^5.2.2"
}
}
30 changes: 25 additions & 5 deletions src/command.js → src/command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import {newNote, getAllNotes, findNotes, removeNote, removeAllNotes} from './notes.js'
import {newNote, getAllNotes, findNotes, removeNote, removeAllNotes} from './notes.ts'
import { start } from './server.js'

const listNotes = (notes) => {
interface Note {
id: number;
content: string;
tags: string[];
}

interface NewNoteArgs {
note: string;
tags?: string;
}

interface FindNotesArgs {
filter: string;
}

interface RemoveNoteArgs {
id: number;
}


const listNotes = (notes: Note[]) => {
notes.forEach(note => {
console.log('\n')
console.log('id: ', note.id)
Expand All @@ -13,7 +33,7 @@ const listNotes = (notes) => {
}

yargs(hideBin(process.argv))
.command('new <note>', 'create a new note', yargs => {
.command<NewNoteArgs>('new <note>', 'create a new note', yargs => {
return yargs.positional('note', {
describe: 'The content of the note you want to create',
type: 'string'
Expand All @@ -32,7 +52,7 @@ yargs(hideBin(process.argv))
const notes = await getAllNotes()
listNotes(notes)
})
.command('find <filter>', 'get matching notes', yargs => {
.command<FindNotesArgs>('find <filter>', 'get matching notes', yargs => {
return yargs.positional('filter', {
describe: 'The search term to filter notes by, will be applied to note.content',
type: 'string'
Expand All @@ -41,7 +61,7 @@ yargs(hideBin(process.argv))
const notes = await findNotes(argv.filter)
listNotes(notes)
})
.command('remove <id>', 'remove a note by id', yargs => {
.command<RemoveNoteArgs>('remove <id>', 'remove a note by id', yargs => {
return yargs.positional('id', {
type: 'number',
description: 'The id of the note you want to remove'
Expand Down
14 changes: 12 additions & 2 deletions src/db.js → src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';

interface Note {
tags: string[];
content: string;
id: number;
}

interface Database {
notes: Note[];
}

const DB_FILE_NAME = 'db.json';

const __filename = fileURLToPath(import.meta.url);
Expand All @@ -19,7 +29,7 @@ export const getDB = async () => {
}
};

export const saveDB = async (db) => {
export const saveDB = async (db: Database) => {
try {
await fs.writeFile(DB_PATH, JSON.stringify(db, null, 2));
return db;
Expand All @@ -28,7 +38,7 @@ export const saveDB = async (db) => {
}
};

export const insert = async (data) => {
export const insert = async (data: Note) => {
try {
const db = await getDB();
db.notes.push(data);
Expand Down
12 changes: 6 additions & 6 deletions src/notes.js → src/notes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { insert, getDB, saveDB } from './db.js'

export const newNote = async (note, tags) => {
export const newNote = async (note: string, tags: string[]) => {
const data = {
tags,
id: Date.now(),
Expand All @@ -14,17 +14,17 @@ export const getAllNotes = async () => {
return db.notes
}

export const findNotes = async (filter) => {
export const findNotes = async (filter: string) => {
const notes = await getAllNotes()
return notes.filter(note => note.content.toLowerCase().includes(filter.toLowerCase()))
return notes.filter((note: { content: string }) => note.content.toLowerCase().includes(filter.toLowerCase()))
}

export const removeNote = async (id) => {
export const removeNote = async (id: number) => {
const notes = await getAllNotes()
const match = notes.find(note => note.id === id)
const match = notes.find((note: { id: number }) => note.id === id)

if (match) {
const newNotes = notes.filter(note => note.id !== id)
const newNotes = notes.filter((note: { id: number }) => note.id !== id)
await saveDB({notes: newNotes})
return id
}
Expand Down
15 changes: 11 additions & 4 deletions src/server.js → src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import fs from 'node:fs/promises'
import http from 'node:http'
import open from 'open'

const interpolate = (html, data) => {
interface Note {
id: number;
content: string;
tags: string[];
}


const interpolate = (html: string, data: { [x: string]: any; notes?: string; }) => {
return html.replace(/\{\{\s*(\w+)\s*\}\}/g, (match, placeholder) => {
return data[placeholder] || '';
});
}

const formatNotes = (notes) => {
const formatNotes = (notes: Note[]) => {
return notes.map(note => {
return `
<div class="note">
Expand All @@ -21,7 +28,7 @@ const formatNotes = (notes) => {
}).join('\n')
}

const createServer = (notes) => {
const createServer = (notes: Note[]) => {
return http.createServer(async (req, res) => {
const HTML_PATH = new URL('./template.html', import.meta.url)
const template = await fs.readFile(HTML_PATH, 'utf-8')
Expand All @@ -32,7 +39,7 @@ const createServer = (notes) => {
});
}

export const start = (notes, port) => {
export const start = (notes: Note[], port: number) => {
const server = createServer(notes)
server.listen(port, () => {
console.log(`Server is listening on port ${port}`);
Expand Down
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "node",
"allowImportingTsExtensions": true,
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}