fixturify-project
Advanced tools
Comparing version 1.7.0 to 1.8.0
443
index.js
@@ -1,267 +0,222 @@ | ||
'use strict'; | ||
const fixturify = require('fixturify'); | ||
const tmp = require('tmp'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
"use strict"; | ||
const fixturify = require("fixturify"); | ||
const tmp = require("tmp"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
tmp.setGracefulCleanup(); | ||
function keys(object) { | ||
if (object !== null && (typeof object === 'object' || Array.isArray(object))) { | ||
return Object.keys(object); | ||
} else { | ||
return []; | ||
} | ||
if (object !== null && (typeof object === 'object' || Array.isArray(object))) { | ||
return Object.keys(object); | ||
} | ||
else { | ||
return []; | ||
} | ||
} | ||
module.exports = class Project { | ||
constructor(name, version = '0.0.0', cb, root) { | ||
this.pkg = { | ||
name, | ||
version, | ||
keywords: [] | ||
}; | ||
this._dependencies = {}; | ||
this._devDependencies = {}; | ||
this.validate(); | ||
this.files = { | ||
'index.js': ` | ||
class Project { | ||
constructor(name, version = '0.0.0', cb, root) { | ||
this.files = { | ||
'index.js': ` | ||
'use strict'; | ||
module.exports = {};` | ||
}; | ||
this.isDependency = true; | ||
if (root) { | ||
this._root = root; | ||
} else { | ||
this._tmp = tmp.dirSync({ unsafeCleanup: true }); | ||
this._root = fs.realpathSync(this._tmp.name); | ||
}; | ||
this.isDependency = true; | ||
this._dependencies = {}; | ||
this._devDependencies = {}; | ||
this.pkg = { | ||
name, | ||
version, | ||
keywords: [] | ||
}; | ||
this.validate(); | ||
if (root) { | ||
this._root = root; | ||
} | ||
else { | ||
this._tmp = tmp.dirSync({ unsafeCleanup: true }); | ||
this._root = fs.realpathSync(this._tmp.name); | ||
} | ||
if (typeof cb === 'function') { | ||
cb(this); | ||
} | ||
} | ||
if (typeof cb === 'function') { | ||
cb(this); | ||
get root() { | ||
return this._root; | ||
} | ||
} | ||
get root() { | ||
return this._root; | ||
} | ||
get baseDir() { | ||
return path.join(this._root, this.name); | ||
} | ||
get name() { | ||
return this.pkg.name; | ||
} | ||
set name(value) { | ||
this.pkg.name = value; | ||
} | ||
get version() { | ||
return this.pkg.version; | ||
} | ||
set version(value) { | ||
this.pkg.version = value; | ||
} | ||
static fromJSON(json, name) { | ||
if (json[name] === undefined) { | ||
throw new Error(`${name} was expected, but not found`); | ||
get baseDir() { | ||
return path.join(this._root, this.name); | ||
} | ||
let files = JSON.parse(JSON.stringify(json[name])); | ||
let pkg = JSON.parse(files['package.json']); | ||
let nodeModules = files['node_modules']; | ||
// drop "special files" | ||
delete files['node_modules']; | ||
delete files['package.json']; | ||
let project = new this(pkg.name, pkg.version); | ||
keys(pkg.dependencies).forEach(dependency => { | ||
project.addDependency(this.fromJSON(nodeModules, dependency)); | ||
}); | ||
keys(pkg.devDependencies).forEach(dependency => { | ||
project.addDevDependency(this.fromJSON(nodeModules, dependency)); | ||
}); | ||
delete pkg.dependencies; | ||
delete pkg.devDependencies; | ||
project.pkg = pkg; | ||
project.files = files; | ||
return project; | ||
} | ||
static fromDir(root, name) { | ||
let project = new this(name, 'x.x.x'); | ||
project.readSync(root); | ||
return project; | ||
} | ||
writeSync(root = this.root) { | ||
fixturify.writeSync(root, this.toJSON()); | ||
} | ||
readSync(root = this.root) { | ||
let files = fixturify.readSync(root)[this.name]; | ||
let pkg = JSON.parse(files['package.json']); | ||
let nodeModules = files['node_modules']; | ||
// drop "special files" | ||
delete files['node_modules']; | ||
delete files['package.json']; | ||
this.name = pkg.name; | ||
this.version = pkg.version; | ||
this.keywords = pkg.keywords; | ||
this._dependencies = {}; | ||
this._devDependencies = {}; | ||
this.files = files; | ||
keys(pkg.dependencies).forEach(dependency => { | ||
this.addDependency(this.constructor.fromJSON(nodeModules, dependency)); | ||
}); | ||
keys(pkg.devDependencies).forEach(dependency => { | ||
this.addDevDependency(this.constructor.fromJSON(nodeModules, dependency)); | ||
}); | ||
} | ||
addDependency(name, version, cb) { | ||
let dep; | ||
if (typeof name === 'string') { | ||
dep = this._dependencies[name] = new this.constructor(name, version, null, path.join(this.root, this.name, 'node_modules')); | ||
} else if (name.isDependency) { | ||
dep = this._dependencies[name.name] = name; | ||
} else { | ||
throw new TypeError('WTF'); | ||
get name() { | ||
return this.pkg.name; | ||
} | ||
if (typeof cb === 'function') { | ||
cb(dep); | ||
set name(value) { | ||
this.pkg.name = value; | ||
} | ||
return dep; | ||
} | ||
removeDependency(name) { | ||
delete this._dependencies[name]; | ||
} | ||
removeDevDependency(name) { | ||
delete this._devDependencies[name]; | ||
} | ||
addDevDependency(name, version, cb) { | ||
let dep; | ||
if (typeof name === 'string') { | ||
dep = this._devDependencies[name] = new this.constructor(name, version, null, path.join(this.root, this.name, 'node_modules')); | ||
} else if (name.isDependency) { | ||
dep = this._devDependencies[name.name] = name; | ||
} else { | ||
throw new TypeError('WTF'); | ||
get version() { | ||
return this.pkg.version; | ||
} | ||
if (typeof cb === 'function') { | ||
cb(dep); | ||
set version(value) { | ||
this.pkg.version = value; | ||
} | ||
return dep; | ||
} | ||
dependencies() { | ||
return Object.keys(this._dependencies).map(dependency => this._dependencies[dependency]); | ||
} | ||
devDependencies() { | ||
return Object.keys(this._devDependencies).map(dependency => this._devDependencies[dependency]); | ||
} | ||
validate() { | ||
if (typeof this.name !== 'string') { | ||
throw new TypeError('Missing name'); | ||
static fromJSON(json, name) { | ||
if (json[name] === undefined) { | ||
throw new Error(`${name} was expected, but not found`); | ||
} | ||
let files = JSON.parse(JSON.stringify(json[name])); | ||
let pkg = JSON.parse(files['package.json']); | ||
let nodeModules = files['node_modules']; | ||
// drop "special files" | ||
delete files['node_modules']; | ||
delete files['package.json']; | ||
let project = new this(pkg.name, pkg.version); | ||
keys(pkg.dependencies).forEach(dependency => { | ||
project.addDependency(this.fromJSON(nodeModules, dependency)); | ||
}); | ||
keys(pkg.devDependencies).forEach(dependency => { | ||
project.addDevDependency(this.fromJSON(nodeModules, dependency)); | ||
}); | ||
delete pkg.dependencies; | ||
delete pkg.devDependencies; | ||
project.pkg = pkg; | ||
project.files = files; | ||
return project; | ||
} | ||
if (typeof this.version !== 'string') { | ||
throw new TypeError(`${this.name} is missing a version`); | ||
static fromDir(root, name) { | ||
let project = new this(name, 'x.x.x'); | ||
project.readSync(root); | ||
return project; | ||
} | ||
this.dependencies().forEach(dep => dep.validate()); | ||
this.devDependencies().forEach(dep => dep.validate()); | ||
} | ||
toJSON() { | ||
if (arguments.length > 0) { | ||
return this.toJSON()[this.name][arguments[0]]; | ||
} else { | ||
return { | ||
[this.name]: Object.assign({}, this.files, { | ||
'node_modules': depsAsObject([ | ||
...this.devDependencies(), | ||
...this.dependencies() | ||
]), | ||
'package.json': JSON.stringify(Object.assign(this.pkg, { | ||
dependencies: depsToObject(this.dependencies()), | ||
devDependencies: depsToObject(this.devDependencies()), | ||
}), null, 2), | ||
}), | ||
}; | ||
writeSync(root = this.root) { | ||
fixturify.writeSync(root, this.toJSON()); | ||
} | ||
} | ||
clone() { | ||
return this.constructor.fromJSON(this.toJSON(), this.name); | ||
} | ||
dispose() { | ||
if (this._tmp) { | ||
this._tmp.removeCallback(); | ||
readSync(root = this.root) { | ||
let files = fixturify.readSync(root)[this.name]; | ||
let pkg = JSON.parse(files['package.json']); | ||
let nodeModules = files['node_modules']; | ||
// drop "special files" | ||
delete files['node_modules']; | ||
delete files['package.json']; | ||
this.name = pkg.name; | ||
this.version = pkg.version; | ||
this._dependencies = {}; | ||
this._devDependencies = {}; | ||
this.files = files; | ||
keys(pkg.dependencies).forEach(dependency => { | ||
this.addDependency(this.constructor.fromJSON(nodeModules, dependency)); | ||
}); | ||
keys(pkg.devDependencies).forEach(dependency => { | ||
this.addDevDependency(this.constructor.fromJSON(nodeModules, dependency)); | ||
}); | ||
} | ||
} | ||
addDependency(name, version, cb) { | ||
let dep; | ||
if (typeof name === 'string') { | ||
dep = this._dependencies[name] = new this.constructor(name, version, undefined, path.join(this.root, this.name, 'node_modules')); | ||
} | ||
else if (name.isDependency) { | ||
dep = this._dependencies[name.name] = name; | ||
} | ||
else { | ||
throw new TypeError('WTF'); | ||
} | ||
if (typeof cb === 'function') { | ||
cb(dep); | ||
} | ||
return dep; | ||
} | ||
removeDependency(name) { | ||
delete this._dependencies[name]; | ||
} | ||
removeDevDependency(name) { | ||
delete this._devDependencies[name]; | ||
} | ||
addDevDependency(name, version, cb) { | ||
let dep; | ||
if (typeof name === 'string') { | ||
dep = this._devDependencies[name] = new this.constructor(name, version, undefined, path.join(this.root, this.name, 'node_modules')); | ||
} | ||
else if (name.isDependency) { | ||
dep = this._devDependencies[name.name] = name; | ||
} | ||
else { | ||
throw new TypeError('WTF'); | ||
} | ||
if (typeof cb === 'function') { | ||
cb(dep); | ||
} | ||
return dep; | ||
} | ||
dependencies() { | ||
return Object.keys(this._dependencies).map(dependency => this._dependencies[dependency]); | ||
} | ||
devDependencies() { | ||
return Object.keys(this._devDependencies).map(dependency => this._devDependencies[dependency]); | ||
} | ||
validate() { | ||
if (typeof this.name !== 'string') { | ||
throw new TypeError('Missing name'); | ||
} | ||
if (typeof this.version !== 'string') { | ||
throw new TypeError(`${this.name} is missing a version`); | ||
} | ||
this.dependencies().forEach(dep => dep.validate()); | ||
this.devDependencies().forEach(dep => dep.validate()); | ||
} | ||
toJSON(key) { | ||
if (key) { | ||
return this.toJSON()[this.name][key]; | ||
} | ||
else { | ||
return { | ||
[this.name]: Object.assign({}, this.files, { | ||
'node_modules': depsAsObject([ | ||
...this.devDependencies(), | ||
...this.dependencies() | ||
]), | ||
'package.json': JSON.stringify(Object.assign(this.pkg, { | ||
dependencies: depsToObject(this.dependencies()), | ||
devDependencies: depsToObject(this.devDependencies()), | ||
}), null, 2), | ||
}), | ||
}; | ||
} | ||
} | ||
clone() { | ||
return this.constructor.fromJSON(this.toJSON(), this.name); | ||
} | ||
dispose() { | ||
if (this._tmp) { | ||
this._tmp.removeCallback(); | ||
} | ||
} | ||
} | ||
function parseScoped(name) { | ||
let matched = name.match(/@([^@\/]+)\/(.*)/); | ||
if (matched) { | ||
return { | ||
scope: matched[1], | ||
name: matched[2], | ||
}; | ||
} | ||
return null; | ||
let matched = name.match(/@([^@\/]+)\/(.*)/); | ||
if (matched) { | ||
return { | ||
scope: matched[1], | ||
name: matched[2], | ||
}; | ||
} | ||
return null; | ||
} | ||
function depsAsObject(modules) { | ||
let obj = {}; | ||
modules.forEach(dep => { | ||
let scoped = parseScoped(dep.name); | ||
if (scoped) { | ||
let root = obj['@' + scoped.scope] = obj['@' + scoped.scope] || {}; | ||
root[scoped.name] = dep.toJSON()[dep.name]; | ||
} else { | ||
obj[dep.name] = dep.toJSON()[dep.name]; | ||
} | ||
}); | ||
return obj; | ||
let obj = {}; | ||
modules.forEach(dep => { | ||
let scoped = parseScoped(dep.name); | ||
if (scoped) { | ||
let root = obj['@' + scoped.scope] = obj['@' + scoped.scope] || {}; | ||
root[scoped.name] = dep.toJSON()[dep.name]; | ||
} | ||
else { | ||
obj[dep.name] = dep.toJSON()[dep.name]; | ||
} | ||
}); | ||
return obj; | ||
} | ||
function depsToObject(deps) { | ||
let obj = {}; | ||
deps.forEach(dep => obj[dep.name] = dep.version); | ||
return obj; | ||
let obj = {}; | ||
deps.forEach(dep => obj[dep.name] = dep.version); | ||
return obj; | ||
} | ||
module.exports = Project; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "fixturify-project", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"main": "index.js", | ||
@@ -13,10 +13,23 @@ "repository": "git@github.com:stefanpenner/node-fixturify-project", | ||
"devDependencies": { | ||
"@types/chai": "^4.1.7", | ||
"@types/fs-extra": "^5.0.5", | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^11.10.4", | ||
"@types/tmp": "^0.0.34", | ||
"chai": "^4.1.2", | ||
"fs-extra": "^7.0.0", | ||
"mocha": "^5.2.0" | ||
"mocha": "^5.2.0", | ||
"typescript": "^3.3.3333" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts", | ||
"index.js.map" | ||
], | ||
"scripts": { | ||
"test": "mocha test", | ||
"test:debug": "mocha debug test" | ||
"test:debug": "mocha debug test", | ||
"prepare": "tsc", | ||
"clean": "git clean -x -f" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 2 instances in 1 package
2
19444
9
5
254
1