feat: create typescript dockerfile and the extra command (#28)

Co-authored-by: EvolutionX
This commit is contained in:
Allyedge
2022-05-29 19:56:06 +02:00
committed by GitHub
parent 765771f759
commit b1a8683373
6 changed files with 80 additions and 1 deletions

12
src/commands/extra.js Normal file
View File

@@ -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);
}

View File

@@ -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);

19
src/prompts/extra.js Normal file
View File

@@ -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,
},
],
};

View File

@@ -0,0 +1,13 @@
FROM node:latest
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
RUN tsc --build
RUN node dist/index.js

33
src/utilities/create.js Normal file
View File

@@ -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);
}

View File

@@ -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);