Skip to content

Commit

Permalink
Format using new standards. (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Nov 6, 2021
1 parent 7017557 commit cb46b68
Show file tree
Hide file tree
Showing 39 changed files with 102 additions and 166 deletions.
1 change: 0 additions & 1 deletion benchmark/env.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* global bench */
'use strict';
const yeoman = require('..');

suite('Environment', () => {
Expand Down
2 changes: 1 addition & 1 deletion cli/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node
const {YeomanCommand} = require('../lib/util/command');
const {printGroupedGenerator, environmentAction} = require('./utils');
const packageJson = require('../package.json');
const Env = require('..');
const {printGroupedGenerator, environmentAction} = require('./utils');

const program = new YeomanCommand();

Expand Down
8 changes: 4 additions & 4 deletions cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const Env = require('..');

const printGroupedGenerator = (generators, env) => {
const grouped = groupBy(generators, 'packagePath');
Object.entries(grouped).forEach(([packagePath, group]) => {
for (const [packagePath, group] of Object.entries(grouped)) {
const namespace = env.toNamespace(group[0].namespace);
console.log(` ${namespace.packageNamespace} at ${packagePath}`);
group.forEach(generator => {
for (const generator of group) {
const generatorNamespace = env.toNamespace(generator.namespace);
console.log(` :${generatorNamespace.generator || 'app'}`);
});
}
console.log('');
});
}
console.log(`${generators.length} generators`);
};

Expand Down
1 change: 0 additions & 1 deletion lib/adapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const inquirer = require('inquirer');
const diff = require('diff');
const chalk = require('chalk');
Expand Down
1 change: 0 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const debug = require('debug')('yeoman:environment:command');
const {Option} = require('commander');

Expand Down
5 changes: 2 additions & 3 deletions lib/composability.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const assert = require('assert');
const semver = require('semver');
const pacote = require('pacote');
Expand Down Expand Up @@ -32,9 +31,9 @@ composability.installLocalGenerators = async function (packages) {
return true;
}
const toInstall = {};
entries.forEach(([packageName, version]) => {
for (const [packageName, version] of entries) {
toInstall[packageName] = version;
});
}
if (await this.repository.installPackages(toInstall)) {
const packagesToLookup = entries.map(([packageName, _]) => packageName);
this.lookupLocalPackages(packagesToLookup);
Expand Down
17 changes: 7 additions & 10 deletions lib/environment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const fs = require('fs');
const path = require('path');
const EventEmitter = require('events');
const crypto = require('crypto');
const chalk = require('chalk');
const _ = require('lodash');
const GroupedQueue = require('grouped-queue');
Expand All @@ -11,7 +11,6 @@ const memFs = require('mem-fs');
const FileEditor = require('mem-fs-editor');
const debug = require('debug')('yeoman:environment');
const isScoped = require('is-scoped');
const crypto = require('crypto');
const npmlog = require('npmlog');
const semver = require('semver');
const {TrackerGroup} = require('are-we-there-yet');
Expand Down Expand Up @@ -44,13 +43,13 @@ function splitArgsFromString(argsString) {
return result;
}
const quoteSeparatedArgs = argsString.split(/("[^"]*")/).filter(x => x);
quoteSeparatedArgs.forEach(arg => {
for (const arg of quoteSeparatedArgs) {
if (arg.match('\x22')) {
result.push(arg.replace(/"/g, ''));
} else {
result = result.concat(arg.trim().split(' '));
}
});
}
return result;
}

Expand Down Expand Up @@ -329,12 +328,12 @@ class Environment extends Base {
this.repository = new YeomanRepository(this.options.yeomanRepository);

if (!this.options.experimental) {
process.argv.forEach(value => {
for (const value of process.argv) {
if (value === '--experimental') {
this.options.experimental = true;
debug('Set environment as experimental');
}
});
}
}

this.loadSharedOptions(this.options);
Expand Down Expand Up @@ -865,9 +864,7 @@ class Environment extends Base {
getAllGenerators() {
return Object.fromEntries([
...Object.entries(this._generators),
...Object.entries(this._generatorsForPath).map(([root, generatorStore]) => {
return Object.entries(generatorStore).map(([namespace, generator]) => ([`${root}#${namespace}`, generator]));
}).flat()
...Object.entries(this._generatorsForPath).flatMap(([root, generatorStore]) => Object.entries(generatorStore).map(([namespace, generator]) => ([`${root}#${namespace}`, generator])))
]);
}

Expand Down Expand Up @@ -1137,7 +1134,7 @@ class Environment extends Base {
let ns = path.normalize(filepath.replace(new RegExp(escapeStrRe(path.extname(filepath)) + '$'), ''));

// Sort lookups by length so biggest are removed first
const nsLookups = _(lookups.concat(['..'])).map(found => path.normalize(found)).sortBy('length').value().reverse();
const nsLookups = _([...lookups, '..']).map(found => path.normalize(found)).sortBy('length').value().reverse();

// If `ns` contains a lookup dir in its path, remove it.
ns = nsLookups.reduce((ns, lookup) => {
Expand Down
1 change: 0 additions & 1 deletion lib/generator-features.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const debug = require('debug')('yeoman:environment:features');

module.exports = cls => class extends cls {
Expand Down
26 changes: 7 additions & 19 deletions lib/namespace-composability.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const debug = require('debug')('yeoman:environment:compose');
const EventEmitter = require('events');
const path = require('path');
Expand Down Expand Up @@ -39,9 +38,7 @@ composability.lookupLocalNamespaces = function (namespacesToLookup) {
namespacesToLookup = Array.isArray(namespacesToLookup) ? namespacesToLookup : [namespacesToLookup];
namespacesToLookup = namespacesToLookup.map(ns => this.requireNamespace(ns));
// Keep only those packages that has a compatible version.
namespacesToLookup = namespacesToLookup.filter(ns => {
return this.repository.verifyInstalledVersion(ns.generatorHint, ns.semver) !== undefined;
});
namespacesToLookup = namespacesToLookup.filter(ns => this.repository.verifyInstalledVersion(ns.generatorHint, ns.semver) !== undefined);
return this.lookupLocalPackages(namespacesToLookup.map(ns => ns.generatorHint));
};

Expand All @@ -66,18 +63,13 @@ composability.lookupNamespaces = function (namespaces, options = {}) {
const genPath = ns.generator.split(':').join('/');
let filePatterns = [`${genPath}/index.?s`, `${genPath}.?s`];
const lookups = options.lookups || this.lookups;
filePatterns = lookups.map(prefix => {
return filePatterns.map(pattern => path.join(prefix, pattern));
}).reduce(
(accumulator, currentValue) => accumulator.concat(currentValue),
[]
);
filePatterns = lookups.flatMap(prefix => filePatterns.map(pattern => path.join(prefix, pattern)));
nsOptions.filePatterns = filePatterns;
nsOptions.singleResult = true;
}
return nsOptions;
});
return options_.map(opt => this.lookup({...opt, ...options})).reduce((acc, cur) => acc.concat(cur), []);
return options_.flatMap(opt => this.lookup({...opt, ...options}));
};

/**
Expand All @@ -100,7 +92,7 @@ composability.prepareEnvironment = async function (namespaces) {

const assertMissing = missing => {
if (missing.length > 0) {
throw new Error(`Error preparing environment for ${missing.map(ns => ns.complete).join()}`);
throw new Error(`Error preparing environment for ${missing.map(ns => ns.complete).join(',')}`);
}
};

Expand Down Expand Up @@ -143,7 +135,7 @@ composability.prepareEnvironment = async function (namespaces) {
return true;
}
// At last, try to lookup if install failed.
this.lookupLocalNamespaces(missing.concat(toLookup));
this.lookupLocalNamespaces([...missing, ...toLookup]);

assertMissing(updateMissing());
return true;
Expand Down Expand Up @@ -276,18 +268,14 @@ class YeomanCompose extends EventEmitter {
* @return {Promise} Promise YeomanWith
*/
async with(namespace, generatorOptions, ...methodArgs) {
let namespaces = [].concat(namespace);
let namespaces = [namespace].flat();
debug(`Compose with generator ${namespace} at ${this._destinationRoot}`);
namespaces = namespaces.map(namespace => this.env.requireNamespace(namespace));
const brokenNamespace = namespaces.find(namespace => !namespace.generator);
if (brokenNamespace) {
throw new Error(`Generator part in namespace is missing: ${brokenNamespace.id}`);
}
const namespacesId = namespaces.map(namespace => {
return namespace.instanceId && namespace.instanceId === '*' ? this._getInstanceNames(namespace.namespace).map(instanceId => {
return namespace.with({instanceId}).id;
}) : [namespace.id];
}).flat();
const namespacesId = namespaces.flatMap(namespace => namespace.instanceId && namespace.instanceId === '*' ? this._getInstanceNames(namespace.namespace).map(instanceId => namespace.with({instanceId}).id) : [namespace.id]);

const composedGenerators = await Promise.all(namespacesId.map(nsId => this.require(nsId, generatorOptions)));
const composedProxy = new Proxy(composedGenerators, multipleGeneratorProxyHandler);
Expand Down
2 changes: 0 additions & 2 deletions lib/namespace.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const YeomanNamespace = require('./util/namespace');

module.exports = cls => class extends cls {
Expand Down
1 change: 0 additions & 1 deletion lib/package-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const path = require('path');
const debug = require('debug')('yeoman:environment:package-manager');
const preferredPm = require('preferred-pm');
Expand Down
8 changes: 3 additions & 5 deletions lib/resolver.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const arrify = require('arrify');
const path = require('path');
const fs = require('fs');
const arrify = require('arrify');
const _ = require('lodash');
const globby = require('globby');
const debug = require('debug')('yeoman:environment');
Expand Down Expand Up @@ -324,8 +323,7 @@ packageLookup._getGlobalNpmPaths = function (filterPaths = true) {
} else if (win32) {
paths.push(path.join(process.env.APPDATA, 'npm/node_modules'));
} else {
paths.push('/usr/lib/node_modules');
paths.push('/usr/local/lib/node_modules');
paths.push('/usr/lib/node_modules', '/usr/local/lib/node_modules');
}

// Add NVM prefix directory
Expand Down Expand Up @@ -406,7 +404,7 @@ resolver.alias = function (match, value) {
return this;
}

const aliases = this.aliases.slice(0).reverse();
const aliases = [...this.aliases].reverse();

return aliases.reduce((resolved, alias) => {
if (!alias.match.test(resolved)) {
Expand Down
1 change: 0 additions & 1 deletion lib/spawn-command.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const spawn = require('execa');

/**
Expand Down
6 changes: 1 addition & 5 deletions lib/store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const debug = require('debug')('yeoman:environment:store');

const {requireOrImport} = require('./util/esm');
Expand Down Expand Up @@ -76,9 +74,7 @@ class Store {
}

if (maybeGenerator.then) {
return maybeGenerator.then(esmGenerator => {
return [esmGenerator, {...this._meta[namespace]}];
});
return maybeGenerator.then(esmGenerator => [esmGenerator, {...this._meta[namespace]}]);
}

Object.assign(maybeGenerator, this._meta[namespace]);
Expand Down
5 changes: 2 additions & 3 deletions lib/util/binary-diff.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';
const Table = require('cli-table');
const fs = require('fs');
const dateFormat = require('dateformat');
const path = require('path');
const Table = require('cli-table');
const dateFormat = require('dateformat');
const prettyBytes = require('pretty-bytes');

const {isBinaryFileSync} = require('isbinaryfile');
Expand Down
4 changes: 2 additions & 2 deletions lib/util/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class YeomanCommand extends Command {
*/
addGeneratorOptions(options) {
options = options || {};
Object.entries(options).forEach(([key, value]) => {
for (const [key, value] of Object.entries(options)) {
this._addGeneratorOption(key, value);
});
}
return this;
}

Expand Down
1 change: 0 additions & 1 deletion lib/util/conflicter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict';
const fs = require('fs');
const path = require('path');
const jsdiff = require('diff');
Expand Down
1 change: 0 additions & 1 deletion lib/util/log.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @module env/log */
'use strict';
const util = require('util');
const EventEmitter = require('events');
const _ = require('lodash');
Expand Down
14 changes: 5 additions & 9 deletions lib/util/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@ module.exports = class YeomanNamespace {
this.generator = parsed.generator;
this.instanceId = parsed.instanceId;
this.semver = parsed.semver;
if (parsed.method) {
this.methods = parsed.method.split('+');
} else {
this.methods = parsed.methods;
}
this.methods = parsed.method ? parsed.method.split('+') : parsed.methods;
this.flags = parsed.flags;

// Populate flags
if (this.flags) {
Object.entries(flags).forEach(([name, value]) => {
for (const [name, value] of Object.entries(flags)) {
if (this.flags === value) {
this[name] = true;
} else {
delete this[name];
}
});
}
}

debug('Parsed namespace %o', this);
Expand All @@ -44,11 +40,11 @@ module.exports = class YeomanNamespace {

const parsed = {complete};
// Populate fields
Object.entries(groups).forEach(([name, value]) => {
for (const [name, value] of Object.entries(groups)) {
if (result[value]) {
parsed[name] = result[value];
}
});
}
return parsed;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/util/out-of-order-transform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {Transform} = require('stream');
const debug = require('debug');
const {default: PQueue} = require('p-queue');
const {Transform} = require('stream');

module.exports = class OOOTransform extends Transform {
/**
Expand Down
13 changes: 6 additions & 7 deletions lib/util/repository.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @module env/repository */
'use strict';
const fs = require('fs');
const path = require('path');
const semver = require('semver');
Expand Down Expand Up @@ -92,9 +91,9 @@ class YeomanRepository {
throw new Error(`Package ${packageName} already loaded`);
}

toCleanup.forEach(cache => {
for (const cache of toCleanup) {
delete require.cache[cache];
});
}
}

/**
Expand Down Expand Up @@ -177,14 +176,14 @@ class YeomanRepository {
try {
await this.reifyRepository({add: [...packagesArgs]});
} catch (error) {
Object.keys(packages).forEach(packageName => {
for (const packageName of Object.keys(packages)) {
this.log.error(`${packageName} cannot be installed. ${error}`);
});
}
return false;
}
Object.keys(packages).forEach(packageName => {
for (const packageName of Object.keys(packages)) {
this.log.ok(`${packageName} installed.`);
});
}
return true;
}

Expand Down
Loading

0 comments on commit cb46b68

Please sign in to comment.