diff --git a/src/actions/init.js b/src/actions/init.js index 29d5973..ad96f4a 100644 --- a/src/actions/init.js +++ b/src/actions/init.js @@ -3,23 +3,39 @@ import jsonfile from 'jsonfile'; import inquirer from 'inquirer'; import { mkdir } from 'lib/fs'; -import { PACKAGE_TYPES, CWD, DOTFILES_FOLDER } from 'constants'; +import { PACKAGE_TYPES, COMPONENT_ENVS, CWD, DOTFILES_FOLDER } from 'constants'; export const generatePackageJson = async ({ packageType, + componentEnv, + packagePublic, packageName, - organisationName, + organisationID, + npmScope, authorDetail, }) => { const TEMPLATE_PATH = `${DOTFILES_FOLDER}/${packageType}/package.json`; const template = await jsonfile.readFile(TEMPLATE_PATH); - template.name = `@${organisationName}/${packageName}`; - template.repository = template.repository - .replace('', packageName) - .replace('', organisationName); + template.name = `@${npmScope}/${packageName}`; + template.repository = `git@github.com:${organisationID}/${packageName}.git`; template.author = authorDetail; + if (packagePublic) { + // make it compatible with semantic-release + delete template.private; + template.publishConfig = { + access: 'public', + }; + } + + if (componentEnv === 'cli') { + delete template.main; + template.bin = { + [packageName]: 'dist/index.js', + }; + } + const TARGET_PATH = `${CWD}/${packageName}/pacakge.json`; await jsonfile.writeFile(TARGET_PATH, template, { spaces: 2, @@ -42,9 +58,23 @@ const questions = [ { type: 'list', name: 'packageType', - message: 'Choose the type of the package:', + message: 'Which type of package do you want to create?', choices: PACKAGE_TYPES, }, + { + type: 'list', + name: 'componentEnv', + message: 'Where the component will be used?', + choices: COMPONENT_ENVS, + when: ({ packageType }) => packageType === 'component', + }, + { + type: 'confirm', + name: 'packagePublic', + message: 'Is the component to be published?', + default: true, + when: ({ packageType }) => packageType === 'component', + }, { type: 'input', name: 'packageName', @@ -52,9 +82,16 @@ const questions = [ }, { type: 'input', - name: 'organisationName', + name: 'organisationID', message: 'Enter the github id of the organisation:', }, + { + type: 'input', + name: 'npmScope', + message: 'Enter the organisation scope name in npm:', + default: ({ organisationID }) => organisationID, + when: ({ packagePublic }) => packagePublic, + }, { type: 'input', name: 'authorDetail', @@ -67,13 +104,24 @@ export default () => inquirer .prompt(questions) .then( - async ({ packageType, packageName, organisationName, authorDetail }) => { + async ({ + packageType, + componentEnv, + packagePublic, + packageName, + organisationID, + npmScope, + authorDetail, + }) => { await mkdir(`${CWD}/${packageName}`); await mkdir(`${CWD}/${packageName}/src`); await generatePackageJson({ packageType, + componentEnv, + packagePublic, packageName, - organisationName, + organisationID, + npmScope, authorDetail, }); await copyConfigFiles({ packageType, packageName }); diff --git a/src/constants.js b/src/constants.js index efae6aa..99eef67 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,6 +1,7 @@ import os from 'os'; export const PACKAGE_TYPES = ['app', 'component']; +export const COMPONENT_ENVS = ['node', 'cli']; export const CWD = process.cwd(); // current working directory: location where node command is invoked export const HOME = os.homedir();