From 25dab12ff1245245f835f3b349aaaf6d3bd39f58 Mon Sep 17 00:00:00 2001 From: stphndev <133531810+stphndev@users.noreply.github.com> Date: Sun, 10 Sep 2023 20:17:58 +0000 Subject: [PATCH] Add typescript to files --- index.js | 2 -- index.ts | 2 ++ package-lock.json | 19 +++++++++++++++++++ package.json | 6 ++++-- src/{command.js => command.ts} | 30 +++++++++++++++++++++++++----- src/{db.js => db.ts} | 14 ++++++++++++-- src/{notes.js => notes.ts} | 12 ++++++------ src/{server.js => server.ts} | 15 +++++++++++---- tsconfig.json | 18 ++++++++++++++++++ 9 files changed, 97 insertions(+), 21 deletions(-) delete mode 100644 index.js create mode 100644 index.ts rename src/{command.js => command.ts} (79%) rename src/{db.js => db.ts} (77%) rename src/{notes.js => notes.ts} (52%) rename src/{server.js => server.ts} (76%) create mode 100644 tsconfig.json diff --git a/index.js b/index.js deleted file mode 100644 index 8fd2d91..0000000 --- a/index.js +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env node -import "./src/command.js" \ No newline at end of file diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..ac97a08 --- /dev/null +++ b/index.ts @@ -0,0 +1,2 @@ +#!/usr/bin/env node +import "./src/command.ts" \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index bb90964..902968c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,12 @@ "jest": "^29.6.4", "open": "^9.1.0", "yargs": "^17.7.2" + }, + "bin": { + "note": "index.js" + }, + "devDependencies": { + "typescript": "^5.2.2" } }, "node_modules/@ampproject/remapping": { @@ -3432,6 +3438,19 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/typescript": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/untildify": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/untildify/-/untildify-4.0.0.tgz", diff --git a/package.json b/package.json index 5d4953c..66a9c3a 100644 --- a/package.json +++ b/package.json @@ -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" @@ -25,5 +24,8 @@ "jest": "^29.6.4", "open": "^9.1.0", "yargs": "^17.7.2" + }, + "devDependencies": { + "typescript": "^5.2.2" } } diff --git a/src/command.js b/src/command.ts similarity index 79% rename from src/command.js rename to src/command.ts index 8839d25..9141d4a 100644 --- a/src/command.js +++ b/src/command.ts @@ -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) @@ -13,7 +33,7 @@ const listNotes = (notes) => { } yargs(hideBin(process.argv)) - .command('new ', 'create a new note', yargs => { + .command('new ', 'create a new note', yargs => { return yargs.positional('note', { describe: 'The content of the note you want to create', type: 'string' @@ -32,7 +52,7 @@ yargs(hideBin(process.argv)) const notes = await getAllNotes() listNotes(notes) }) - .command('find ', 'get matching notes', yargs => { + .command('find ', 'get matching notes', yargs => { return yargs.positional('filter', { describe: 'The search term to filter notes by, will be applied to note.content', type: 'string' @@ -41,7 +61,7 @@ yargs(hideBin(process.argv)) const notes = await findNotes(argv.filter) listNotes(notes) }) - .command('remove ', 'remove a note by id', yargs => { + .command('remove ', 'remove a note by id', yargs => { return yargs.positional('id', { type: 'number', description: 'The id of the note you want to remove' diff --git a/src/db.js b/src/db.ts similarity index 77% rename from src/db.js rename to src/db.ts index e5a7a8a..6a0b3f0 100644 --- a/src/db.js +++ b/src/db.ts @@ -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); @@ -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; @@ -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); diff --git a/src/notes.js b/src/notes.ts similarity index 52% rename from src/notes.js rename to src/notes.ts index 3f4d224..cfdc692 100644 --- a/src/notes.js +++ b/src/notes.ts @@ -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(), @@ -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 } diff --git a/src/server.js b/src/server.ts similarity index 76% rename from src/server.js rename to src/server.ts index 94bc49c..a3d7ec0 100644 --- a/src/server.js +++ b/src/server.ts @@ -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 `
@@ -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') @@ -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}`); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..2dd6789 --- /dev/null +++ b/tsconfig.json @@ -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"] + } + \ No newline at end of file