Created
July 16, 2022 21:05
-
-
Save guiseek/08b2847451738728828401d8a0a3f710 to your computer and use it in GitHub Desktop.
Create an app inside a npm workspace. Params: directory name. Ex: ts-node create-workspace-app.ts apps mobile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { execSync } from 'child_process' | |
const [directory, filename] = process.argv.slice(2, 4) | |
const log = (...args: any[]) => console.log(`log: `, ...args) | |
const copyFile = (src: string, to: string) => { | |
execSync(`cp ${src} ${to}`) | |
log(`file ${src} copied to ${to}`) | |
} | |
const createDir = (value: string) => { | |
execSync(`mkdir ${value}`) | |
log(`directory ${value} created`) | |
} | |
const createFile = (value: string) => { | |
execSync(`touch ${value}`) | |
log(`file ${value} created`) | |
} | |
const handleJson = <T extends Record<string, string>>( | |
dir: string, | |
name: string, | |
obj: T | |
) => { | |
const cmd = `json -I -f ${dir}/${name}/package.json -e` | |
Object.entries(obj).forEach(([key, value]) => { | |
execSync(`${cmd} "this.${key} = '${value}';"`) | |
}) | |
} | |
function createApp(dir: string, name: string) { | |
execSync(`npm init -w ./${dir}/${name} -y`) | |
log(`workspace ${dir}/${name} initialized`) | |
createDir(`${dir}/${name}/src`) | |
createFile(`${dir}/${name}/src/main.ts`) | |
createFile(`${dir}/${name}/src/styles.scss`) | |
const files = ['index.html', 'vite.svg', 'tsconfig.json'] | |
files.forEach((file) => { | |
const src = `scripts/files/${file}` | |
const to = `${dir}/${name}/${file}` | |
copyFile(src, to) | |
}) | |
handleJson(directory, filename, { | |
'scripts.start': 'vite', | |
'scripts.build': 'tsc && vite build', | |
'scripts.preview': 'vite preview', | |
}) | |
log(`file ${dir}/${name}package.json updated`) | |
} | |
try { | |
if (!directory) throw new Error('Missing directory') | |
if (!filename) throw new Error('Missing name') | |
if (directory && filename) { | |
createApp(directory, filename) | |
} | |
} catch (err) { | |
console.error(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment