fixturify-project
Advanced tools
Comparing version 1.10.0 to 2.0.0
@@ -1,7 +0,6 @@ | ||
interface DirJSON { | ||
[filename: string]: DirJSON | string; | ||
} | ||
import fixturify from 'fixturify'; | ||
import { PackageJson } from 'type-fest'; | ||
declare class Project { | ||
pkg: any; | ||
files: DirJSON; | ||
pkg: PackageJson; | ||
files: fixturify.DirJSON; | ||
readonly isDependency = true; | ||
@@ -13,7 +12,9 @@ private _dependencies; | ||
constructor(name: string, version?: string, cb?: (project: Project) => void, root?: string); | ||
readonly root: string; | ||
readonly baseDir: string; | ||
name: string; | ||
version: string; | ||
static fromJSON(json: DirJSON, name: string): Project; | ||
get root(): string; | ||
get baseDir(): string; | ||
get name(): string; | ||
set name(value: string); | ||
get version(): string; | ||
set version(value: string); | ||
static fromJSON(json: fixturify.DirJSON, name: string): Project; | ||
static fromDir(root: string, name?: string): Project; | ||
@@ -29,4 +30,4 @@ writeSync(root?: string): void; | ||
validate(): void; | ||
toJSON(): DirJSON; | ||
toJSON(key: string): DirJSON | string; | ||
toJSON(): fixturify.DirJSON; | ||
toJSON(key: string): fixturify.DirJSON | string; | ||
clone(): Project; | ||
@@ -33,0 +34,0 @@ dispose(): void; |
101
index.js
"use strict"; | ||
const fixturify = require("fixturify"); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
const fixturify_1 = __importDefault(require("fixturify")); | ||
const tmp = require("tmp"); | ||
@@ -7,2 +10,5 @@ const fs = require("fs"); | ||
tmp.setGracefulCleanup(); | ||
function deserializePackageJson(serialized) { | ||
return JSON.parse(serialized); | ||
} | ||
function keys(object) { | ||
@@ -16,2 +22,61 @@ if (object !== null && (typeof object === 'object' || Array.isArray(object))) { | ||
} | ||
function getString(obj, properyName, errorMessage) { | ||
const value = obj[properyName]; | ||
if (typeof value === 'string') { | ||
return value; | ||
} | ||
else { | ||
throw new TypeError(errorMessage || `expected 'string' but got '${typeof value}'`); | ||
} | ||
} | ||
function cloneDirJSON(serialized) { | ||
return JSON.parse(JSON.stringify(serialized)); | ||
} | ||
/** | ||
A utility method access a file from a DirJSON that is typesafe and runtime safe. | ||
```ts | ||
getFile(folder, 'package.json') // the files content, or it will throw | ||
``` | ||
*/ | ||
function getFile(dir, fileName) { | ||
const value = dir[fileName]; | ||
if (typeof value === 'string') { | ||
return value; | ||
} | ||
else if (typeof value === 'object' && value !== null) { | ||
throw new TypeError(`Expected a file for name '${fileName}' but got a 'Folder'`); | ||
} | ||
else { | ||
throw new TypeError(`Expected a file for name '${fileName}' but got '${typeof value}'`); | ||
} | ||
} | ||
/** | ||
A utility method access a file from a DirJSON that is typesafe and runtime safe | ||
```ts | ||
getFolder(folder, 'node_modules') // => the DirJSON of folder['node_module'] or it will throw | ||
``` | ||
*/ | ||
function getFolder(dir, fileName) { | ||
const value = dir[fileName]; | ||
if (isDirJSON(value)) { | ||
return value; | ||
} | ||
else if (typeof value === 'string') { | ||
throw new TypeError(`Expected a file for name '${fileName}' but got 'File'`); | ||
} | ||
else { | ||
throw new TypeError(`Expected a folder for name '${fileName}' but got '${typeof value}'`); | ||
} | ||
} | ||
function isDirJSON(value) { | ||
return typeof value === 'object' && value !== null; | ||
} | ||
function getPackageName(pkg) { | ||
return getString(pkg, 'name', `package.json is missing a name.`); | ||
} | ||
function getPackageVersion(pkg) { | ||
return getString(pkg, 'version', `${getPackageName(pkg)}'s package.json is missing a version.`); | ||
} | ||
class Project { | ||
@@ -51,3 +116,3 @@ constructor(name, version = '0.0.0', cb, root) { | ||
get name() { | ||
return this.pkg.name; | ||
return getPackageName(this.pkg); | ||
} | ||
@@ -58,3 +123,3 @@ set name(value) { | ||
get version() { | ||
return this.pkg.version; | ||
return getPackageVersion(this.pkg); | ||
} | ||
@@ -65,12 +130,10 @@ set version(value) { | ||
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']; | ||
const folder = getFolder(json, name); | ||
let files = cloneDirJSON(folder); | ||
let pkg = deserializePackageJson(getFile(files, 'package.json')); | ||
let nodeModules = getFolder(files, 'node_modules'); | ||
// drop "special files" | ||
delete files['node_modules']; | ||
delete files['package.json']; | ||
let project = new this(pkg.name, pkg.version); | ||
let project = new this(getPackageName(pkg), pkg.version); | ||
keys(pkg.dependencies).forEach(dependency => { | ||
@@ -90,4 +153,4 @@ project.addDependency(this.fromJSON(nodeModules, dependency)); | ||
if (arguments.length === 1) { | ||
const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'UTF8')); | ||
let project = new this(pkg.name, pkg.version, undefined, path.dirname(root)); | ||
const pkg = deserializePackageJson(fs.readFileSync(path.join(root, 'package.json'), 'UTF8')); | ||
const project = new this(getPackageName(pkg), pkg.version, undefined, path.dirname(root)); | ||
project.readSync(); | ||
@@ -107,8 +170,8 @@ return project; | ||
writeSync(root = this.root) { | ||
fixturify.writeSync(root, this.toJSON()); | ||
fixturify_1.default.writeSync(root, this.toJSON()); | ||
} | ||
readSync(root = this.root) { | ||
let files = unwrapPackageName(fixturify.readSync(root), this.name); | ||
this.pkg = JSON.parse(files['package.json']); | ||
let nodeModules = files['node_modules']; | ||
const files = unwrapPackageName(fixturify_1.default.readSync(root), this.name); | ||
this.pkg = deserializePackageJson(getFile(files, 'package.json')); | ||
const nodeModules = getFolder(files, 'node_modules'); | ||
// drop "special files" | ||
@@ -173,3 +236,3 @@ delete files['node_modules']; | ||
if (typeof this.name !== 'string') { | ||
throw new TypeError('Missing name'); | ||
throw new TypeError('missing name'); | ||
} | ||
@@ -233,5 +296,5 @@ if (typeof this.version !== 'string') { | ||
if (scoped) { | ||
return obj[scoped.scope][scoped.name]; | ||
return getFolder(getFolder(obj, scoped.scope), scoped.name); | ||
} | ||
return obj[packageName]; | ||
return getFolder(obj, packageName); | ||
} | ||
@@ -238,0 +301,0 @@ function wrapPackageName(packageName, value) { |
{ | ||
"name": "fixturify-project", | ||
"version": "1.10.0", | ||
"version": "2.0.0", | ||
"main": "index.js", | ||
@@ -9,15 +9,17 @@ "repository": "git@github.com:stefanpenner/node-fixturify-project", | ||
"dependencies": { | ||
"fixturify": "^1.2.0", | ||
"tmp": "^0.0.33" | ||
"fixturify": "^2.0.0", | ||
"tmp": "^0.0.33", | ||
"type-fest": "^0.11.0" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.1.7", | ||
"@types/fs-extra": "^5.0.5", | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^11.10.4", | ||
"@types/chai": "^4.2.9", | ||
"@types/fs-extra": "^8.1.0", | ||
"@types/mocha": "^7.0.1", | ||
"@types/node": "^13.7.4", | ||
"@types/tmp": "^0.0.34", | ||
"chai": "^4.1.2", | ||
"fs-extra": "^7.0.0", | ||
"mocha": "^5.2.0", | ||
"typescript": "^3.3.3333" | ||
"chai": "^4.2.0", | ||
"fs-extra": "^8.1.0", | ||
"mocha": "^7.0.1", | ||
"source-map-support": "^0.5.16", | ||
"typescript": "^3.8.2" | ||
}, | ||
@@ -30,7 +32,10 @@ "files": [ | ||
"scripts": { | ||
"test": "mocha test", | ||
"test:debug": "mocha debug test", | ||
"test": "mocha test -r source-map-support/register", | ||
"test:debug": "mocha debug test -r source-map-support/register", | ||
"prepare": "tsc", | ||
"clean": "git clean -x -f" | ||
}, | ||
"engines": { | ||
"node": "10.* || >= 12.*" | ||
} | ||
} |
@@ -1,4 +0,3 @@ | ||
# node-fixturify-project | ||
[![Build Status](https://travis-ci.org/stefanpenner/node-fixturify-project.svg?branch=master)](https://travis-ci.org/stefanpenner/node-fixturify-project) | ||
[![Build status](https://ci.appveyor.com/api/projects/status/li9y4rjfjt7fmvpc/branch/master?svg=true)](https://ci.appveyor.com/project/embercli/node-fixturify-project/branch/master) | ||
# node-fixturify-project | ||
![CI](https://github.com/stefanpenner/node-fixturify-project/workflows/CI/badge.svg) | ||
@@ -29,2 +28,3 @@ A complementary project to [fixturify](https://github.com/joliss/node-fixturify) | ||
project.pkg // => the contents of package.json for the given project | ||
project.files['index.js'] = 'module.exports = "Hello, World!"'; | ||
@@ -93,2 +93,3 @@ | ||
* `Project.prototype.root` the path that `Project.prototype.writeSync` will write to by default, if not specified as constructor argument will default to a temp directory | ||
* `Project.prototype.pkg` the projects `package.json` represented as an in-memory POJO. | ||
* `Project.prototype.files` a POJO (in the format of `fixturify`) representing the files within the project root | ||
@@ -95,0 +96,0 @@ * `Project.prototype.clone()` deep clone a given project |
Sorry, the diff of this file is not supported yet
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
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
24900
335
100
3
10
+ Addedtype-fest@^0.11.0
+ Added@types/fs-extra@8.1.5(transitive)
+ Addedensure-posix-path@1.1.1(transitive)
+ Addedfixturify@2.1.1(transitive)
+ Addedfs-extra@8.1.0(transitive)
+ Addedtype-fest@0.11.0(transitive)
+ Addedwalk-sync@2.2.0(transitive)
- Removed@types/fs-extra@5.1.0(transitive)
- Removedfixturify@1.3.0(transitive)
- Removedfs-extra@7.0.1(transitive)
Updatedfixturify@^2.0.0