diff --git a/src/commands/extra.js b/src/commands/extra.js new file mode 100644 index 0000000..9eb9e8f --- /dev/null +++ b/src/commands/extra.js @@ -0,0 +1,12 @@ +import prompts from 'prompts'; +import { extraPrompt } from '../prompts/extra.js'; +import { create } from '../utilities/create.js'; +const { prompt } = prompts; + +export async function extra() { + const extra = await prompt([extraPrompt]); + + if (Object.keys(extra).length < 1) process.exit(1); + const lang = extra.extra.includes('typescript') ? 'TS' : 'JS'; + await create(extra.extra.split('-')[0], lang, process.cwd(), true); +} diff --git a/src/index.js b/src/index.js index 65453c1..f65f28b 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import { init } from './commands/init.js'; import { help } from './commands/help.js'; +import { extra } from './commands/extra.js'; const regex = /(?<=--|-)\w+/gm; const rawArgs = process.argv.slice(2); @@ -19,6 +20,7 @@ const commands = new Map([ ['help', help], ['', help], ['init', init], + ['extra', extra], ]); const found = commands.get(cmdName); diff --git a/src/prompts/extra.js b/src/prompts/extra.js new file mode 100644 index 0000000..2e9dbaf --- /dev/null +++ b/src/prompts/extra.js @@ -0,0 +1,19 @@ +export const extraPrompt = { + message: 'What extra feature do you want to add?', + name: 'extra', + type: 'select', + choices: [ + { + title: 'Dockerfile (TypeScript)', + description: 'Dockerfile for TypeScript', + value: 'Dockerfile-typescript', + selected: true, + }, + { + title: 'Dockerfile (JavaScript)', + description: 'Dockerfile for JavaScript', + value: 'Dockerfile-javascript', + disabled: true, + }, + ], +}; diff --git a/src/templates/extra/Dockerfile.TS.sern b/src/templates/extra/Dockerfile.TS.sern new file mode 100644 index 0000000..fe30972 --- /dev/null +++ b/src/templates/extra/Dockerfile.TS.sern @@ -0,0 +1,13 @@ +FROM node:latest + +WORKDIR /app + +COPY package.json ./ + +RUN npm install + +COPY . . + +RUN tsc --build + +RUN node dist/index.js \ No newline at end of file diff --git a/src/utilities/create.js b/src/utilities/create.js new file mode 100644 index 0000000..1a7bdfb --- /dev/null +++ b/src/utilities/create.js @@ -0,0 +1,33 @@ +import { URL, fileURLToPath } from 'url'; +import { resolve, dirname } from 'node:path'; +import { readFile, mkdir, writeFile } from 'fs/promises'; +const root = new URL('../', import.meta.url); + +const templates = new URL('./templates/', root); +const extraURL = new URL('./extra/', templates); +const extraFolder = fileURLToPath(extraURL); + +export async function create(name, lang, location, no_ext) { + const file = `${name}.${lang}.sern`; + + const target = no_ext ? `${location}/${name}` : `${location}/${name}.${lang}`; + + return createFile(file, target); +} + +async function createFile(template, target) { + const location = `${extraFolder}${template}`; + + const file = await readFile(location, 'utf8'); + + await writeFileRecursive(target, file); +} + +async function writeFileRecursive(target, data) { + const resolvedTarget = resolve(target); + const dir = dirname(resolvedTarget); + + await mkdir(dir, { recursive: true }); + + return writeFile(resolvedTarget, data); +} diff --git a/src/utilities/install.js b/src/utilities/install.js index 0880b8b..e2e5266 100644 --- a/src/utilities/install.js +++ b/src/utilities/install.js @@ -71,7 +71,7 @@ export async function cloneRepo(lang, name) { * @param {string} src - The source path. * @param {string} dest - The destination folder where the files will be copied to. */ -function copyRecursiveSync(src, dest) { +export function copyRecursiveSync(src, dest) { const exists = fs.existsSync(src); const stats = exists && fs.statSync(src);