Skip to content

Commit

Permalink
feat(init): can create component with cli and public specific settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyulin committed Nov 21, 2018
1 parent 646fd11 commit 7b00e60
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
68 changes: 58 additions & 10 deletions src/actions/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<package-name>', packageName)
.replace('<organisation>', 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,
Expand All @@ -42,19 +58,40 @@ 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',
message: 'Enter the name of the package:',
},
{
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',
Expand All @@ -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 });
Expand Down
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down

1 comment on commit 7b00e60

@zhenyulin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.