| 
1 | 1 | #!/usr/bin/env node  | 
2 | 2 | 
 
  | 
 | 3 | +const fs = require('fs')  | 
3 | 4 | const path = require('path')  | 
4 | 5 | const fse = require('fs-extra')  | 
 | 6 | +const signale = require('signale')  | 
5 | 7 | const { program } = require('commander')  | 
6 | 8 | 
 
  | 
7 |  | -const templatePath = path.resolve(__dirname, '../template')  | 
 | 9 | +// path of template.  | 
 | 10 | +const template = path.resolve(__dirname, '../template')  | 
 | 11 | + | 
 | 12 | +// change package name of a node package.  | 
 | 13 | +// @param {string} name - new name of node package.  | 
 | 14 | +// @param {string} path - path of package file node package.  | 
 | 15 | +const change_package = (name, path) => {  | 
 | 16 | +  try {  | 
 | 17 | +    const package_data = require(path)  | 
 | 18 | +    package_data.name = name  | 
 | 19 | +    fs.writeFileSync(path, JSON.stringify(package_data, null, 2))  | 
 | 20 | +  } catch (error) {  | 
 | 21 | +    signale.error(new Error('Apologize for failed to write package file.'));  | 
 | 22 | +  }  | 
 | 23 | +}  | 
8 | 24 | 
 
  | 
9 | 25 | // function to copy template files.  | 
10 |  | -const createApp = async (appName) => {  | 
11 |  | -  const destPath = path.resolve(process.cwd(), appName)  | 
 | 26 | +async function create_app(app_name) {  | 
 | 27 | +  const destination = path.resolve(process.cwd(), app_name)  | 
12 | 28 |   try {  | 
13 |  | -    await fse.copy(templatePath, destPath)  | 
14 |  | -    console.log(`[+] Express app has been created at ${destPath}...`)  | 
 | 29 | +    await fse.copy(template, destination)  | 
 | 30 | +    signale.success(`Copying template files to ${app_name} is successful.`)  | 
 | 31 | +    change_package(app_name, `${destination}/package.json`)  | 
 | 32 | +    signale.success(`Resolving name of package is successful.`)  | 
15 | 33 |   } catch (error) {  | 
16 |  | -    console.error(`[-] ${error.message}`)  | 
 | 34 | +    signale.error(error)  | 
17 | 35 |   }  | 
18 | 36 | }  | 
19 | 37 | 
 
  | 
 | 38 | +// set arguments and actions to create app.  | 
20 | 39 | program  | 
21 | 40 |   .version('1.0.0')  | 
22 |  | -  .argument('<app-name>', 'Name of the application')  | 
23 |  | -  .action(async (appName) => {  | 
 | 41 | +  .argument('<app-name>', 'name to create new app.')  | 
 | 42 | +  .action(async (app_name) => {  | 
 | 43 | +    signale.time()  | 
24 | 44 |     // check if folder already exist.  | 
25 |  | -    if (fse.existsSync(appName)) {  | 
26 |  | -      console.error('[-] Express is app already exist...')  | 
27 |  | -      return  | 
 | 45 | +    if (!fse.existsSync(app_name)) {  | 
 | 46 | +      signale.await(`${app_name} app is on the way.`)  | 
 | 47 | +      await create_app(app_name)  | 
 | 48 | +      signale.complete(`A new app has been created as ${app_name}.`)  | 
 | 49 | +    } else {  | 
 | 50 | +      signale.error(`${app_name} app does exist in same folder.`)  | 
28 | 51 |     }  | 
29 |  | -    await createApp(appName)  | 
 | 52 | +    signale.timeEnd()  | 
30 | 53 |   })  | 
31 | 54 | 
 
  | 
 | 55 | +// exection of process.  | 
32 | 56 | program.parse(process.argv)  | 
0 commit comments