Skip to content

Commit

Permalink
fix(setup): move dotfiles generation under setup
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyulin committed Nov 28, 2018
1 parent 4fd569f commit 40c890f
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 113 deletions.
72 changes: 3 additions & 69 deletions src/actions/init/steps.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import cpy from 'cpy';
import jsonfile from 'jsonfile';
import replace from 'replace-in-file';
import yaml from 'write-yaml';

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

export const createDir = async ({ packageName }) => {
const PACKAGE_DIR = `${CWD}/${packageName}`;
await mkdir(PACKAGE_DIR);
await mkdir(`${PACKAGE_DIR}/src`);
await writeFile(`${PACKAGE_DIR}/src/index.js`, '');
};
import { CWD } from 'constants';

export const createNcmrc = async ({
packageType,
Expand All @@ -30,6 +20,7 @@ export const createNcmrc = async ({
},
package: {
type: packageType,
template: '',
name: packageName,
description: '',
keywords: '',
Expand All @@ -46,7 +37,6 @@ export const createNcmrc = async ({
npm: organisationNpm,
},
};
console.log(template);
const NCMRC_PATH = `${CWD}/${packageName}/.ncmrc.yml`;
yaml.sync(NCMRC_PATH, template, {
safe: true,
Expand All @@ -60,61 +50,5 @@ export const addCommentsToNcmrc = async ({ packageName }) => {
await writeFile(NCMRC_PATH, updated);
};

export const generatePackageJson = async ({
packageType,
componentEnv,
packagePublic,
packageName,
organisationID,
npmScope,
authorDetail,
}) => {
const TEMPLATE_PATH = `${DOTFILES_FOLDER}/${packageType}/package.json`;
const template = await jsonfile.readFile(TEMPLATE_PATH);

template.name = `@${npmScope}/${packageName}`;
template.repository = `[email protected]:${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}/package.json`;
await jsonfile.writeFile(TARGET_PATH, template, {
spaces: 2,
});
};

export const copyConfigFiles = async ({ packageType, packageName }) =>
cpy(
[
`${DOTFILES_FOLDER}/common/*`,
`${DOTFILES_FOLDER}/common/.*`,
`${DOTFILES_FOLDER}/${packageType}/*`,
`${DOTFILES_FOLDER}/${packageType}/.*`,
`!${DOTFILES_FOLDER}/${packageType}/package.json`, // don't copy the package.json
],
`./${packageName}`,
);

export const updateReadme = async ({ packageName, organisationID, npmScope }) =>
replace({
files: `${CWD}/${packageName}/README.md`,
from: [/{{packageName}}/g, /{{organisationID}}/g, /{{npmScope}}/g],
to: [packageName, organisationID, npmScope],
});

export const initGit = async ({ packageName }) =>
exec(`git init -q ${CWD}/${packageName}`);
44 changes: 0 additions & 44 deletions src/actions/setup.js

This file was deleted.

19 changes: 19 additions & 0 deletions src/actions/setup/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as steps from './steps';

export default async () => {
const config = await steps.readConfig();

await steps.cloneTemplateRepo(config);

await steps.copyTemplateFiles();

await steps.removeTemplateDir();

await steps.updatePackageJson(config);

// await steps.generateReadme(config);

await steps.createGithubRepo(config);

await steps.addGitRemoteOrigin(config);
};
48 changes: 48 additions & 0 deletions src/actions/setup/package-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const configPackageJsonFromTemplate = (config, template) => {
const output = Object.assign({}, template);
if (config.package.type === 'component') {
output.name = `@${config.owner.npm}/${config.package.name}`;
output.description = config.package.description;
output.keywords = config.package.keywords;

// cleanup possible fields
delete output.private;
delete output.license;
delete output.publishConfig;
delete output.main;
delete output.bin;

if (!config.package.private) {
// make it compatible with semantic-release
output.publishConfig = {
access: 'public',
};
output.license = 'MIT'; // TODO: read license in config
} else {
output.private = true;
}

if (config.component.environment === 'cli') {
output.bin = {
[config.package.name]: 'dist/index.js',
};
} else {
output.main = 'dist/index.js';
}
} else {
delete output.main; // entry point specified in Dockerfile
delete output.description; // only useful in npm
delete output.keywords; // only useful in npm
delete output.engine; // only useful for component
output.name = config.package.name;
output.private = true;
}
output.repository = `[email protected]:${config.owner.github}/${
config.package.name
}.git`;
output.author = `${config.owner.name} <${config.owner.email}>`;

return output;
};

export default configPackageJsonFromTemplate;
88 changes: 88 additions & 0 deletions src/actions/setup/steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import cpy from 'cpy';
import cosmiconfig from 'cosmiconfig';
import jsonfile from 'jsonfile';
// 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';

// TODO: setup schema and sanitise config file
export const readConfig = async () => {
const MODULE_NAME = 'ncm';
const { config } = await cosmiconfig(MODULE_NAME).search();
return config;
};

export const cloneTemplateRepo = async config => {
const DEFAULT_TEMPLATE = `opbi/ncm-template-${config.package.type}`;
const template = config.package.template || DEFAULT_TEMPLATE;
await exec(`rm -rf .template`);
await exec(`git clone [email protected]:${template}.git .template`);
};

export const copyTemplateFiles = async () => {
await cpy(
[
'.template/*',
'.template/.*',
'!.template/.ncmrc.yml', // there can be .ncmrc.yml in template
],
'.',
);
await exec('cp -r .template/src .');
await exec('cp -r .template/.circle .');
};

export const removeTemplateDir = async () => exec('rm -rf .template');

export const updatePackageJson = async config => {
const PACKAGE_JSON_PATH = './package.json';
const template = await jsonfile.readFile(PACKAGE_JSON_PATH);
const packageJson = configPackageJsonFromTemplate(config, template);
await jsonfile.writeFile(PACKAGE_JSON_PATH, packageJson, {
spaces: 2,
});
};

// 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 createGithubRepo = async config => {
const github = await setupGithubClient();
await github.repos.createInOrg({
org: config.owner.github,
name: config.package.name,
description: config.package.description,
private: config.package.private,
});
};

export const addGitRemoteOrigin = async config =>
exec(
`git add remote origin git@[email protected]:${config.owner.github}/${
config.package.name
}`,
);

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

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

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

// export const initGitCommit = async () => {};

// export const installDeps = async () => {};

// export const initGitPush = async () => {};
17 changes: 17 additions & 0 deletions src/lib/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import octokit from '@octokit/rest';

export const setupGithubClient = async ({ authRequired = false }) => {
const github = octokit();
if (authRequired) {
if (process.env.NCM_AUTH_GITHUB_TOKEN) {
throw Error('NCM_AUTH_GITHUB_TOKEN not found in environment');
}
await github.authenticate({
type: 'oauth',
token: process.env.NCM_AUTH_GITHUB_TOKEN,
});
}
return github;
};

export default {};

0 comments on commit 40c890f

Please sign in to comment.