Skip to content

Commit

Permalink
feat(ncm setup): can read dotfiles from template repo
Browse files Browse the repository at this point in the history
feat(ncm setup): generate README, package.json, create GitHun repo and push to GitHub
fix(ncm init): will cd into project dir
  • Loading branch information
zhenyulin committed Nov 29, 2018
1 parent 49aa296 commit f201288
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/actions/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export default () =>
const { componentType, componentName } = answers;
await steps.addCommentsToNcmrc({ componentName });
await steps.initGit({ componentName });
await steps.cdToComponentDir({ componentName });
console.log(
`created new [${componentType}]: ./${componentName}\ncheck .ncmrc.yml and run 'ncm setup'`,
`created new [${componentType}]: ${componentName}\ncheck .ncmrc.yml and run 'ncm setup'`,
);
});
3 changes: 3 additions & 0 deletions src/actions/init/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ export const addCommentsToNcmrc = async ({ componentName }) => {

export const initGit = async ({ componentName }) =>
exec(`git init -q ./${componentName}`);

export const cdToComponentDir = async ({ componentName }) =>
exec(`cd ./${componentName}`);
7 changes: 5 additions & 2 deletions src/actions/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export default async () => {

await steps.updatePackageJson(config);

// await steps.generateReadme(config);
await steps.generateReadme(config);

await steps.createGithubRepo(config);
console.log(`GitHub repo created`);
// await steps.addGitRemoteOrigin(config);

await steps.addGitRemoteOrigin(config);

await steps.commitAndPushToGitHub();
};
55 changes: 39 additions & 16 deletions src/actions/setup/steps.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import cpy from 'cpy';
import cosmiconfig from 'cosmiconfig';
import jsonfile from 'jsonfile';
// import replace from 'replace-in-file';
import replace from 'replace-in-file';

// import { CWD } from 'constants';
import { setupGithubClient } from 'lib/github';
import { exec } from 'lib/child-process';

import configPackageJsonFromTemplate from './package-json';
import configPackageJsonFromTemplate from 'lib/package-json';

// TODO: setup schema and sanitise config file
export const readConfig = async () => {
Expand All @@ -17,7 +16,7 @@ export const readConfig = async () => {
};

export const cloneTemplateRepo = async config => {
const DEFAULT_TEMPLATE = `opbi/ncm-art-${config.component.type}`;
const DEFAULT_TEMPLATE = `opbi/ncm-preset-${config.component.type}`;
const template = config.component.template || DEFAULT_TEMPLATE;
await exec(`rm -rf .template`);
await exec(`git clone git@github.com:${template}.git .template`);
Expand All @@ -29,8 +28,12 @@ export const copyTemplateFiles = async () => {
'.template/*',
'.template/.*',
'!.template/.ncmrc.yml', // there can be .ncmrc.yml in template
'!.template/README.md',
],
'.',
{
rename: fileName => (fileName === 'TEMPLATE.md' ? 'README.md' : fileName),
},
);
await exec('cp -r .template/src .');
await exec('cp -r .template/.circleci .');
Expand All @@ -47,16 +50,30 @@ export const updatePackageJson = async config => {
});
};

// export const generateReadme = async ({
// packageName,
// organisationID,
// npmScope,
// }) =>
// replace({
// files: `${CWD}/${packageName}/README.md`,
// from: [/{{packageName}}/g, /{{organisationID}}/g, /{{npmScope}}/g],
// to: [packageName, organisationID, npmScope],
// });
export const generateReadme = async config => {
const PACKAGE_JSON_PATH = './package.json';
const {
name: packageJsonName,
license: packageJsonLicense,
} = await jsonfile.readFile(PACKAGE_JSON_PATH);
await replace({
files: `./README.md`,
from: [
/{{componentName}}/g,
/{{componentDescription}}/g,
/{{ownerGithub}}/g,
/{{packageJsonName}}/g,
/{{packageJsonLicense}}/g,
],
to: [
config.component.name,
config.component.description,
config.owner.github,
packageJsonName,
packageJsonLicense,
],
});
};

export const createGithubRepo = async config => {
const authRequired =
Expand All @@ -72,11 +89,17 @@ export const createGithubRepo = async config => {

export const addGitRemoteOrigin = async config =>
exec(
`git add remote origin git@git@github.com:${config.owner.github}/${
`git remote add origin git@github.com:${config.owner.github}/${
config.component.name
}`,
}.git`,
);

export const commitAndPushToGitHub = async () => {
await exec('git add .');
await exec(`git commit -m 'chore: init'`);
await exec('git push -u origin master');
};

// export const setupCIPipeline = async config => {};

// export const setupCoveralls = async config => {};
Expand Down
5 changes: 2 additions & 3 deletions src/actions/setup/package-json.js → src/lib/package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ const configPackageJsonFromTemplate = (config, template) => {
output.author = `${config.owner.name} <${config.owner.email}>`;

if (!config.component.private) {
// make it compatible with semantic-release
output.license = config.package.license || 'MIT';
output.license = config.package.license || template.license;
output.publishConfig = {
access: 'public',
};
}; // make it compatible with semantic-release
} else {
output.private = true;
}
Expand Down

0 comments on commit f201288

Please sign in to comment.