Skip to content

Commit

Permalink
refactor: update publish tooling to be nicer to people publishing for…
Browse files Browse the repository at this point in the history
…ge itself
  • Loading branch information
MarshallOfSound committed Jun 27, 2018
1 parent 34c46ed commit 4137bda
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 51 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"esdoc-standard-plugin": "^1.0.0",
"fetch-mock": "^6.0.0",
"generate-changelog": "^1.0.2",
"listr": "^0.14.1",
"mocha": "^5.0.0",
"nodemon": "^1.11.0",
"nyc": "^11.0.0",
Expand Down
93 changes: 59 additions & 34 deletions tools/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,67 @@ const childProcess = require('child_process');
const fs = require('fs-extra');
const path = require('path');
const spawnPromise = require('cross-spawn-promise');
const Listr = require('listr');

const BASE_DIR = path.resolve(__dirname, '..');
const PACKAGES_DIR = path.resolve(BASE_DIR, 'packages');

(async () => {
// Check clean working dir
if (childProcess.execSync('git status -s', {
cwd: BASE_DIR,
}).toString() !== '') {
throw 'Your working directory is not clean, please ensure you have a clean working directory before publishing'.red;
}
const publisher = new Listr([
{
title: 'Checking working directory',
task: async () => {
if (childProcess.execSync('git status -s', {
cwd: BASE_DIR,
}).toString() !== '') {
throw new Error('Your working directory is not clean, please ensure you have a clean working directory before publishing'.red);
}
},
},
{
title: 'Building all packages',
task: () => spawnPromise('bolt', ['build'], {
cwd: BASE_DIR,
}),
},
{
title: 'Fetching README\'s',
task: require('./sync-readmes'),
},
{
title: 'Collecting directories to publish',
task: async (ctx) => {
ctx.dirsToPublish = [];
for (const subDir of await fs.readdir(PACKAGES_DIR)) {
for (const packageDir of await fs.readdir(path.resolve(PACKAGES_DIR, subDir))) {
ctx.dirsToPublish.push(path.resolve(PACKAGES_DIR, subDir, packageDir));
}
}
},
},
{
title: 'Publishing all packages',
task: async (ctx) => {
return new Listr(await Promise.all(ctx.dirsToPublish.map(async (dir) => {
const { name, version } = await fs.readJson(path.resolve(dir, 'package.json'));
const isBeta = version.includes('beta');
return {
title: `Publishing: ${`${name}@${version}`.cyan} (beta=${isBeta ? 'true'.green : 'red'.red})`,
task: async () => {
try {
await spawnPromise('npm', ['publish', `--access=public${isBeta ? ' --tag=beta' : ''}`], {
cwd: dir,
});
} catch (err) {
throw new Error(`Failed to publish ${`${name}@${version}`.cyan} \n${err.stderr.toString()}`);
}
},
};
})), { concurrent: 5, exitOnError: false });
},
},
]);

console.info('Building all packages');
await spawnPromise('bolt', ['build'], {
cwd: BASE_DIR,
});

console.info('Fetching README\'s');
await require('./sync-readmes')();

console.info('Publishing all packages');

const dirsToPublish = [];

for (const subDir of await fs.readdir(PACKAGES_DIR)) {
for (const packageDir of await fs.readdir(path.resolve(PACKAGES_DIR, subDir))) {
dirsToPublish.push(path.resolve(PACKAGES_DIR, subDir, packageDir));
}
}

for (const dir of dirsToPublish) {
const { name, version } = await fs.readJson(path.resolve(dir, 'package.json'));
const isBeta = version.includes('beta');
console.info(` * Publishing: ${`${name}@${version}`.cyan} (beta=${isBeta ? 'true'.green : 'red'.red})`);
childProcess.execSync(`npm publish --access=public${isBeta ? ' --tag=beta' : ''}`, {
cwd: dir,
});
}
})().catch(console.error);
publisher.run().catch((err) => {
console.error(err);
process.exit(1);
});
44 changes: 31 additions & 13 deletions tools/sync-readmes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fetch = require('node-fetch');
const fs = require('fs-extra');
const Listr = require('listr');
const path = require('path');

const workspaceMappings = {
Expand Down Expand Up @@ -32,25 +33,42 @@ const sanitize = (gb) => {
});
};

const sync = async () => {
for (const workspace of Object.keys(workspaceMappings)) {
const workspaceDir = path.resolve(BASE_DIR, 'packages', workspace);
const sync = () => {
return new Listr([
{
title: 'Collecting package keys',
task: async (ctx) => {
ctx.packageKeys = [];

for (const packageName of await fs.readdir(path.resolve(workspaceDir))) {
const packageKey = workspaceMappings[workspace][packageName] || packageName;
for (const workspace of Object.keys(workspaceMappings)) {
const workspaceDir = path.resolve(BASE_DIR, 'packages', workspace);

const r = await fetch(`${DOCS_BASE}/${workspace}s/${packageKey}.md`);
if (r.status !== 200) continue;
for (const packageName of await fs.readdir(path.resolve(workspaceDir))) {
const packageKey = workspaceMappings[workspace][packageName] || packageName;

console.info('Writing README for:', `${path.basename(workspaceDir)}/${packageKey}`);
const md = sanitize(await r.text());
await fs.writeFile(path.resolve(workspaceDir, packageName, 'README.md'), md);
}
}
ctx.packageKeys.push([workspace, workspaceDir, packageKey, packageName]);
}
}
},
},
{
title: 'Fetching READMEs',
task: (ctx) => new Listr(ctx.packageKeys.map(([workspace, workspaceDir, packageKey, packageName]) => ({
title: `Fetching README for ${path.basename(workspaceDir)}/${packageKey}`,
task: async () => {
const r = await fetch(`${DOCS_BASE}/${workspace}s/${packageKey}.md`);
if (r.status !== 200) return;

const md = sanitize(await r.text());
await fs.writeFile(path.resolve(workspaceDir, packageName, 'README.md'), md);
},
})), { concurrent: 5 }),
},
]);
};

if (process.mainModule === module) {
sync().catch(console.error);
sync().run().catch(console.error);
}

module.exports = sync;
Loading

0 comments on commit 4137bda

Please sign in to comment.