Skip to content

Commit

Permalink
feat(init): ncm init now only creates .ncmrc.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyulin committed Nov 28, 2018
1 parent f11c244 commit 4fd569f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 32 deletions.
2 changes: 0 additions & 2 deletions dotfiles/component/.ncmrc.yml

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"inquirer": "^6.2.0",
"jsonfile": "^5.0.0",
"node-vault": "^0.9.0",
"replace-in-file": "^3.4.2"
"replace-in-file": "^3.4.2",
"write-yaml": "^1.0.0"
},
"devDependencies": {
"@babel/cli": "^7.1.5",
Expand Down
46 changes: 21 additions & 25 deletions src/actions/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const questions = [
type: 'confirm',
name: 'packagePublic',
message: 'Is the component to be public?',
default: true,
default: ({ packageType }) => packageType === 'component',
when: ({ packageType }) => packageType === 'component',
},
{
Expand All @@ -32,20 +32,21 @@ const questions = [
},
{
type: 'input',
name: 'organisationID',
message: 'Enter the github id of the organisation:',
name: 'organisationGithub',
message: 'Enter the organisation GitHub id:',
},
{
type: 'input',
name: 'npmScope',
message: 'Enter the organisation scope name in npm:',
default: ({ organisationID }) => organisationID,
when: ({ packagePublic }) => packagePublic,
name: 'organisationNpm',
message: 'Enter the organisation npm scope name:',
default: ({ organisationGithub }) => organisationGithub,
when: ({ packageType, packagePublic }) =>
packageType === 'component' && packagePublic,
},
{
type: 'input',
name: 'authorDetail',
message: 'enter the name <contact-email> of the package',
name: 'contactEmail',
message: 'enter email of the primary maintainer:',
// TODO: add validation function
},
];
Expand All @@ -59,28 +60,23 @@ export default () =>
componentEnv,
packagePublic,
packageName,
organisationID,
npmScope,
authorDetail,
organisationGithub,
organisationNpm,
contactEmail,
}) => {
await steps.createDir({ packageName });
await steps.generatePackageJson({
await steps.createNcmrc({
packageType,
componentEnv,
packagePublic,
packageName,
organisationID,
npmScope,
authorDetail,
});
await steps.copyConfigFiles({ packageType, packageName });
await steps.updateReadme({
packageType,
packageName,
organisationID,
npmScope,
organisationGithub,
organisationNpm,
contactEmail,
});
await steps.addCommentsToNcmrc({ packageName });
await steps.initGit({ packageName });
console.log(`created new [${packageType}] package ./${packageName}`);
console.log(
`created new [${packageType}]: ./${packageName}\ncheck .ncmrc.yml and run 'ncm setup'`,
);
},
);
49 changes: 48 additions & 1 deletion src/actions/init/steps.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import cpy from 'cpy';
import jsonfile from 'jsonfile';
import replace from 'replace-in-file';
import yaml from 'write-yaml';

import { mkdir, writeFile } from 'lib/fs';
import { mkdir, readFile, writeFile } from 'lib/fs';
import { exec } from 'lib/child-process';
import { CWD, DOTFILES_FOLDER } from 'constants';

Expand All @@ -13,6 +14,52 @@ export const createDir = async ({ packageName }) => {
await writeFile(`${PACKAGE_DIR}/src/index.js`, '');
};

export const createNcmrc = async ({
packageType,
componentEnv,
packagePublic,
packageName,
organisationGithub,
organisationNpm,
contactEmail,
}) => {
const template = {
language: {
type: 'node',
version: 8,
},
package: {
type: packageType,
name: packageName,
description: '',
keywords: '',
private: !packagePublic,
},
...(packageType === 'component'
? { component: { environment: componentEnv } }
: {}),
owner: {
github: organisationGithub,
team: '',
author: '',
email: contactEmail,
npm: organisationNpm,
},
};
console.log(template);
const NCMRC_PATH = `${CWD}/${packageName}/.ncmrc.yml`;
yaml.sync(NCMRC_PATH, template, {
safe: true,
});
};

export const addCommentsToNcmrc = async ({ packageName }) => {
const NCMRC_PATH = `${CWD}/${packageName}/.ncmrc.yml`;
const template = await readFile(NCMRC_PATH);
const updated = `## created by @opbi/ncm\n---\n${template}`;
await writeFile(NCMRC_PATH, updated);
};

export const generatePackageJson = async ({
packageType,
componentEnv,
Expand Down
5 changes: 3 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os from 'os';

export const PACKAGE_TYPES = ['app', 'component'];
export const COMPONENT_ENVS = ['node', 'cli'];
export const PACKAGE_TYPES = ['component', 'service', 'app', 'job'];
export const COMPONENT_ENVS = ['node', 'cli', 'browser', 'universal'];
export const OWNERSHIP_TYPES = ['organisation', 'personal'];

export const CWD = process.cwd(); // current working directory: location where node command is invoked
export const HOME = os.homedir();
Expand Down
24 changes: 23 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3395,6 +3395,11 @@ from2@^2.1.0, from2@^2.1.1:
inherits "^2.0.1"
readable-stream "^2.0.0"

fs-exists-sync@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/fs-exists-sync/-/fs-exists-sync-0.1.0.tgz#982d6893af918e72d08dec9e8673ff2b5a8d6add"
integrity sha1-mC1ok6+RjnLQjeyehnP/K1qNat0=

fs-extra@^7.0.0:
version "7.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9"
Expand Down Expand Up @@ -4826,7 +4831,7 @@ js-tokens@^3.0.2:
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
integrity sha1-mGbfOVECEw449/mWvOtlRDIJwls=

js-yaml@^3.11.0, js-yaml@^3.12.0, js-yaml@^3.7.0, js-yaml@^3.9.0:
js-yaml@^3.11.0, js-yaml@^3.12.0, js-yaml@^3.7.0, js-yaml@^3.8.3, js-yaml@^3.9.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.12.0.tgz#eaed656ec8344f10f527c6bfa1b6e2244de167d1"
integrity sha512-PIt2cnwmPfL4hKNwqeiuz4bKfnzHTBv6HyVgjahA6mPLwPDzjDWrplJBMjHUFxku/N3FlmrbyPclad+I+4mJ3A==
Expand Down Expand Up @@ -8614,13 +8619,30 @@ write-file-atomic@^2.0.0, write-file-atomic@^2.1.0, write-file-atomic@^2.3.0:
imurmurhash "^0.1.4"
signal-exit "^3.0.2"

write-yaml@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/write-yaml/-/write-yaml-1.0.0.tgz#314596119d0db91247cbc835bce0033b6e0ba09e"
integrity sha1-MUWWEZ0NuRJHy8g1vOADO24LoJ4=
dependencies:
extend-shallow "^2.0.1"
js-yaml "^3.8.3"
write "^0.3.3"

write@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/write/-/write-0.2.1.tgz#5fc03828e264cea3fe91455476f7a3c566cb0757"
integrity sha1-X8A4KOJkzqP+kUVUdvejxWbLB1c=
dependencies:
mkdirp "^0.5.1"

write@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/write/-/write-0.3.3.tgz#09cdc5a2155607ee279f45e38d91ae29fb6a5178"
integrity sha1-Cc3FohVWB+4nn0XjjZGuKftqUXg=
dependencies:
fs-exists-sync "^0.1.0"
mkdirp "^0.5.1"

ws@^5.2.0:
version "5.2.2"
resolved "https://registry.yarnpkg.com/ws/-/ws-5.2.2.tgz#dffef14866b8e8dc9133582514d1befaf96e980f"
Expand Down

0 comments on commit 4fd569f

Please sign in to comment.