Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"eol-last": [
"error",
"unix"
],
"no-tabs": [
"error"
]
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ node_modules/
playground/
yarn.lock
/npm-debug.log

\.vscode/
package-lock.json
44 changes: 20 additions & 24 deletions bin/mondo.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
#!/usr/bin/env node
const fs = require('fs');
const Path = require('path');
const chalk = require('chalk');
const File = require('phylo');
const isDebug = /-{1,2}de?b?u?g?\b/.test(process.argv[2]);
const log = require('loog')({
prefixStyle: 'ascii',
logLevel: isDebug ? 'debug' : 'info'
});

let cwd = Path.resolve('.');
let Mondo, mondoIndex;
let cwd = File.cwd().upToDir('node_modules');
let Mondo, mondoCli;

while (cwd) {
mondoIndex = Path.resolve(cwd, "node_modules/mondorepo/src/cli.js");
if (fs.existsSync(mondoIndex)) {
Mondo = require(mondoIndex);
mondoCli = cwd.join("mondorepo/cli/index.js");
if (mondoCli.exists()) {
log.debug(`Using local version from ${mondoCli.path}`);
Mondo = require(mondoCli.path);
break;
} else {
let parentDir = Path.resolve(cwd, '..');

if (parentDir === cwd) {
cwd = null;
} else {
cwd = parentDir;
}
cwd = cwd.parent && cwd.parent.parent ? cwd.parent.parent.parent.upToDir('node_modules') : null;
}
}

if (!Mondo) {
Mondo = require('../src/cli.js');
mondoCli = File.from(__dirname).parent.join("cli/index.js").absolutify();
log.debug(`Using global version from ${mondoCli.path}`);
Mondo = require(mondoCli.path);
}

const mondo = new Mondo();
mondo.run().then(function (){},
function (cause) {
console.log("");
console.error(chalk.red(mondo.params.debug ? cause : cause.message));
process.exit(1);
}
);
new Mondo(log).run().catch(e => {
log.error(isDebug ? e.stack : (e.message ? e.message : e));
process.exit(1);
});
120 changes: 71 additions & 49 deletions src/commands/Publish.js → cli/Publish.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
"use strict";
const Path = require('path');
const fs = require('fs');
const {Command} = require('switchit');
const BaseCommand = require('./base/command');

const File = require('phylo');
const semver = require('semver');
const chalk = require('chalk');
const columnify = require('columnify');
const jsonfile = require('jsonfile');
const JSON5 = require('json5');
const NPM = require('../pkgMgrs/Npm.js');
const Repo = require('../Repo.js');
const Collection = require('../utils/Collection.js');
const isWindows = /^win/.test(process.platform);

class Publish extends Command {
constructor() {
this.publisher = new NPM();
}
const Npm = require('../src/npm.js');
const Repo = require('../src/repo.js');
const Collection = require('../src/collection.js');

const isWindows = /^win/.test(process.platform);

class Publish extends BaseCommand {
execute(params) {
const {recursive, dry, script, 'check-existing': checkExisting} = params;
const path = params.path ? Path.isAbsolute(params.path) ? params.path : Path.join(process.cwd(), params.path) : process.cwd();
let me = this;

const {recursive, 'dry-run': dry, script, 'check-existing': checkExisting} = params;
const path = params.path ? File.from(params.path).isAbsolute() ? File.from(params.path) : File.cwd().join(params.path) : File.cwd();
const repo = Repo.open(path);

this._packages = new Collection();
me.publisher = new Npm({
log: me.log,
debug: me.root().params.debug
});

me._packages = new Collection();

this.hasPublishConflict = false;
this.dry = script ? false : dry;
this.script = script;
this.checkExisting = checkExisting;
me.hasPublishConflict = false;
me.dry = script ? false : dry;
me.script = script;
me.checkExisting = checkExisting;

// Get a list of all the packages we will be publishing
for (let pkg of (recursive ? repo.allPackages : repo.packages)) {
if (!pkg.private) {
this._packages.add(pkg);
me._packages.add(pkg);
}
}

if (checkExisting) {
return this.doCheckExisting()
return me.doCheckExisting()
.then(() => {
if (this.dry || this.hasPublishConflict) {
this.log();
if (me.dry || me.hasPublishConflict) {
me.writeSummary();
} else if (script) {
this.writeScript();
me.writeScript();
} else {
this.log();
return this.publish();
me.writeSummary();
return me.publish();
}
});
} else {
if (script) {
this.writeScript();
me.writeScript();
} else {
this.log();
return this.publish();
me.writeSummary();
return me.publish();
}
}
}
Expand Down Expand Up @@ -91,7 +94,8 @@ class Publish extends Command {
}));
}

log() {
writeSummary () {
let me = this;
let columns = Array.from(this._packages.items);
let statusRegExp = /^ (W|E) /g;
let statusRegExpResult, colorFunc;
Expand All @@ -109,11 +113,28 @@ class Publish extends Command {
}
});

let colwidth = ((process.stdout.columns - 3) / 3);
columns = columnify(columns, {
showHeaders: false,
minWidth: 20,
showHeaders: true,
minWidth: colwidth,
maxLineWidth: 'auto',
config: {
status: {align: 'center', minWidth: 3}
status: {
align: 'center',
headingTransform: () => 'S\n···',
minWidth: 3
},
name: {
headingTransform: () => chalk.bold('Name')+'\n····',
maxWidth: colwidth
},
version: {
headingTransform: () => chalk.bold('Version')+'\n·······',
maxWidth: colwidth
},
details: {
headingTransform: () => chalk.bold('Details')+'\n·······'
}
},
columns: ['status', 'name', 'version', 'details']
});
Expand All @@ -127,24 +148,23 @@ class Publish extends Command {
return colorFunc(row);
}
return row;
}).join('\n');
});

console.log(columns);
columns.forEach(l => me.log.log(l));
}

publish() {
// Shortcut to chain then's of promises from an array
return this._packages.reduce((promise, pkg) => {
return promise.then(() => {
const json = pkg.publishify();
const original = fs.readFileSync(pkg.file);
jsonfile.writeFileSync(pkg.file, json, {spaces: 4});

const original = pkg.file.load();
jsonfile.writeFileSync(pkg.file.path, json, {spaces: 4});
return this.publisher.publish(pkg.path).then(r => {
fs.writeFileSync(pkg.file, original);
pkg.file.save(original);
return r;
}).catch(err => {
fs.writeFileSync(pkg.file, original);
pkg.file.save(original);
if (this.checkExisting || !err.message.includes('You cannot publish over the previously published version')) {
throw err;
} else {
Expand All @@ -164,27 +184,29 @@ class Publish extends Command {
}

writeScript() {
let me = this;
const prefix = isWindows ? 'REM' : '#';
this._packages.forEach(pkg => {
if (!pkg.$$alreadyPublished) {
console.log(`npm publish ${pkg.path}`);
me.log.log(`npm publish ${pkg.path}`);
} else {
console.log(`${prefix} Version already exists for ${pkg.name}`);
console.log(`${prefix} npm publish ${pkg.path}`);
me.log.log(`${prefix} Version already exists for ${pkg.name}`);
me.log.log(`${prefix} npm publish ${pkg.path}`);
}
});
}
}

Publish.define({
help: {
'': 'Rev version of packages from the current repo'
'': 'Rev version of packages from the current repo',
'dry-run': 'Show a summary of changes to perform, leaves everything intact',
'script': 'Outputs a script to perform the operations manually',
'check-existing': 'Compare against published versions in the npm registry',
'recursive': 'Process all known packages (including those inside used repositories)'
},
parameters: '[path=]',
switches: `[dry:boolean=false]
[script:boolean=false]
[check-existing:boolean=true]
[recursive:boolean=false]`
switches: '[dry-run:boolean=false] [script:boolean=false] [check-existing:boolean=true] [recursive:boolean=false]'
});


Expand Down
Loading