-
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @mixin | ||
* @alias actions/packege-json | ||
*/ | ||
module.exports = (cls) => | ||
class extends cls { | ||
/** | ||
* @private | ||
* Resolve the dependencies to be added to the package.json. | ||
* | ||
* @param {Object|string|string[]} dependencies | ||
* @return {Promise} a 'packageName: packageVersion' object | ||
*/ | ||
async _resolvePackageJsonDependencies(dependencies) { | ||
if (typeof dependencies === 'string') { | ||
dependencies = [dependencies]; | ||
} else if (typeof dependencies !== 'object') { | ||
throw new TypeError( | ||
'resolvePackageJsonDependencies requires an object' | ||
); | ||
} else if (!Array.isArray(dependencies)) { | ||
return dependencies; | ||
} | ||
|
||
const entries = await Promise.all( | ||
dependencies.map((dependency) => this.env.resolvePackage(dependency)) | ||
); | ||
return Object.fromEntries(entries); | ||
} | ||
|
||
/** | ||
* @private | ||
* Add dependencies to be added to the package.json. | ||
* | ||
* @param {Object|string|string[]} dependencies | ||
* @return {Promise} a 'packageName: packageVersion' object | ||
*/ | ||
async addDependencies(dependencies) { | ||
dependencies = await this._resolvePackageJsonDependencies(dependencies); | ||
this.packageJson.merge({dependencies}); | ||
return dependencies; | ||
} | ||
|
||
async addDevDependencies(devDependencies) { | ||
devDependencies = await this._resolvePackageJsonDependencies( | ||
devDependencies | ||
); | ||
this.packageJson.merge({devDependencies}); | ||
return devDependencies; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
'use strict'; | ||
const assert = require('assert'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const makeDir = require('make-dir'); | ||
const rimraf = require('rimraf'); | ||
const Environment = require('yeoman-environment'); | ||
|
||
const Base = require('..'); | ||
|
||
const tmpdir = path.join(os.tmpdir(), 'yeoman-package-json'); | ||
|
||
describe('Base#package-json', function () { | ||
this.timeout(10000); | ||
let generator; | ||
let env; | ||
|
||
beforeEach(function () { | ||
this.prevCwd = process.cwd(); | ||
this.tmp = tmpdir; | ||
makeDir.sync(path.join(tmpdir, 'subdir')); | ||
process.chdir(tmpdir); | ||
|
||
env = Environment.createEnv(); | ||
const Generator = class extends Base {}; | ||
Generator.prototype.exec = function () {}; | ||
generator = new Generator({ | ||
env | ||
}); | ||
}); | ||
|
||
afterEach(function (done) { | ||
process.chdir(this.prevCwd); | ||
rimraf(tmpdir, done); | ||
}); | ||
|
||
describe('_resolvePackageJsonDependencies()', () => { | ||
it('should accept semver version', async () => { | ||
assert.deepStrictEqual( | ||
await generator._resolvePackageJsonDependencies('yeoman-generator@^2'), | ||
{'yeoman-generator': '^2'} | ||
); | ||
}); | ||
|
||
it('should accept github repository', async () => { | ||
assert.deepStrictEqual( | ||
await generator._resolvePackageJsonDependencies( | ||
'yeoman/generator#v4.13.0' | ||
), | ||
{'yeoman-generator': 'github:yeoman/generator#v4.13.0'} | ||
); | ||
}); | ||
|
||
it('should accept github repository version', async () => { | ||
assert.deepStrictEqual( | ||
await generator._resolvePackageJsonDependencies( | ||
'yeoman-generator@yeoman/generator#v4.13.0' | ||
), | ||
{'yeoman-generator': 'github:yeoman/generator#v4.13.0'} | ||
); | ||
}); | ||
|
||
it('should accept object and return it', async () => { | ||
const a = {}; | ||
assert.strictEqual(await generator._resolvePackageJsonDependencies(a), a); | ||
}); | ||
|
||
it('should accept arrays', async () => { | ||
assert.deepStrictEqual( | ||
await generator._resolvePackageJsonDependencies([ | ||
'yeoman-generator@^2', | ||
'yeoman-environment@^2' | ||
]), | ||
{'yeoman-generator': '^2', 'yeoman-environment': '^2'} | ||
); | ||
}); | ||
}); | ||
|
||
describe('addDependencies()', () => { | ||
it('should generate dependencies inside package.json', async () => { | ||
await generator.addDependencies('yeoman-generator@^2'); | ||
assert.deepStrictEqual(generator.packageJson.getAll(), { | ||
dependencies: {'yeoman-generator': '^2'} | ||
}); | ||
}); | ||
|
||
it('should accept object and merge inside package.json', async () => { | ||
await generator.addDependencies({'yeoman-generator': '^2'}); | ||
assert.deepStrictEqual(generator.packageJson.getAll(), { | ||
dependencies: {'yeoman-generator': '^2'} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('addDependencies()', () => { | ||
it('should generate dependencies inside package.json', async () => { | ||
await generator.addDevDependencies('yeoman-generator@^2'); | ||
assert.deepStrictEqual(generator.packageJson.getAll(), { | ||
devDependencies: {'yeoman-generator': '^2'} | ||
}); | ||
}); | ||
|
||
it('should accept object and merge devDependencies inside package.json', async () => { | ||
await generator.addDevDependencies({'yeoman-generator': '^2'}); | ||
assert.deepStrictEqual(generator.packageJson.getAll(), { | ||
devDependencies: {'yeoman-generator': '^2'} | ||
}); | ||
}); | ||
}); | ||
}); |