Detalhes do pacote

fixturify-project

stefanpenner2mMIT7.1.3

readme (leia-me)

node-fixturify-project

npm version CI

When implementing JS build tooling it is common to have complete projects as fixture data. Unfortunately fixtures committed to disk can be somewhat hard to maintain and augment.

Basic Usage

npm install --save-dev fixturify-project
const { Project } = require('fixturify-project');
const project = new Project('rsvp', '3.1.4', {
  files: {
    'index.js': 'module.exports = "Hello, World!"',
  },
});

project.addDependency('mocha', '5.2.0');
project.addDependency('chai', '5.2.0');

project.pkg; // => the contents of package.json for the given project
project.files; // => read or write the set of files further

// if you don't set this, a new temp dir will be made for you when you write()
project.baseDir = 'some/base/dir/';

await project.write();

// after write(), you can read project.baseDir even if you didn't set it
expect(fs.existsSync(join(project.baseDir, 'index.js'))).to.eql(true);

The above example produces the following files (and most importantly the appropriate file contents:

some/base/dir/package.json
some/base/dir/index.js
some/base/dir/node_modules/mocha/package.json
some/base/dir/node_modules/chai/package.json

Nesting Dependencies

addDependency returns another Project instance, so you can nest arbitrarily deep:

const { Project } = require('fixturify-project');

let project = new Project('rsvp');
let a = project.addDependency('a');
let b = a.addDependency('b');
let c = b.addDependency('c');

await project.write();

Which produces:

$TMPDIR/xxx/package.json
$TMPDIR/xxx/index.js
$TMPDIR/xxx/node_modules/a/package.json
$TMPDIR/xxx/node_modules/a/node_modules/b/package.json
$TMPDIR/xxx/node_modules/b/node_modules/b/node_modules/c/package.json

Linking to real dependencies

Instead of creating all packages from scratch, you can link to real preexisting packages. This lets you take a real working package and modify it and its dependencies and watch how it behaves.

const { Project } = require('fixturify-project');

let project = new Project();
let a = project.addDependency('a');

// explicit target
project.linkDependency('b', { target: '/example/b' });

// this will follow node resolution rules to lookup "c" from "../elsewhere"
project.linkDependency('c', { baseDir: '/example' });

// this will follow node resolution rules to lookup "my-aliased-name" from "../elsewhere"
project.linkDependency('d', { baseDir: '/example', resolveName: 'my-aliased-name' });

await project.write();

Produces:

$TMPDIR/xxx/package.json
$TMPDIR/xxx/index.js
$TMPDIR/xxx/node_modules/a/package.json
$TMPDIR/xxx/node_modules/a/node_modules/b -> /example/b
$TMPDIR/xxx/node_modules/b/node_modules/c -> /example/node_modules/c
$TMPDIR/xxx/node_modules/b/node_modules/d -> /example/node_modules/my-aliased-name

When constructing a whole Project from a directory, you can choose to link all dependencies instead of copying them in as Projects:

let project = Project.fromDir('./sample-project', { linkDeps: true });
project.files['extra.js'] = '// stuff';
await project.write();

This will generate a new copy of sample-project, with symlinks to all its original dependencies, but with "extra.js" added.

By default, linkDeps will only link up dependencies (which is appropriate for libraries). If you want to also include devDependencies (which is appropriate for apps) you can use linkDevDeps instead.

API

Project

Kind: global class

project.baseDir

Sets the base directory of the project.

Kind: instance property of Project

Param Description
dir

The directory path.

project.baseDir

Gets the base directory path, usually a tmp directory unless a baseDir has been explicitly set.

Kind: instance property of Project Read only: true

project.name : string

Gets the package name from the package.json.

Kind: instance property of Project

project.name

Sets the package name in the package.json.

Kind: instance property of Project

project.version : string

Gets the version number from the package.json.

Kind: instance property of Project

project.version

Sets the version number in the package.json.

Kind: instance property of Project

project.mergeFiles(dirJSON)

Merges an object containing a directory represention with the existing files.

Kind: instance method of Project

Param Description
dirJSON

An object containing a directory representation to merge.

project.write(dirJSON?)

Writes the existing files property containing a directory representation to the tmp directory.

Kind: instance method of Project

Param Description
dirJSON?

An optional object containing a directory representation to write.

project.addDependency() ⇒

Adds a dependency to the Project's package.json.

Kind: instance method of Project Returns:

  • The Project instance.

project.addDevDependency() ⇒

Adds a devDependency to the Project's package.json.

Kind: instance method of Project Returns:

  • The Project instance.

project.removeDependency(name)

Removes a dependency to the Project's package.json.

Kind: instance method of Project

Param Description
name

The name of the dependency to remove.

project.removeDevDependency(name)

Removes a devDependency.

Kind: instance method of Project

Param Description
name

The name of the devDependency to remove.

project.linkDependency(name)

Links a dependency.

Kind: instance method of Project

Param Description
name

The name of the dependency to link.

project.linkDevDependency(name)

Links a devDependency.

Kind: instance method of Project

Param Description
name

The name of the dependency to link.

project.dependencyProjects() ⇒

Kind: instance method of Project Returns:

  • An array of the dependencies for this Projct.

project.devDependencyProjects() ⇒

Kind: instance method of Project Returns:

  • An array of the devDependencies for this Projct.

project.clone() ⇒

Kind: instance method of Project Returns:

  • The cloned Project.

project.dispose()

Disposes of the tmp directory that the Project is stored in.

Kind: instance method of Project

Project.fromDir(baseDir, opts) ⇒

Reads an existing project from the specified base dir.

Kind: static method of Project Returns:

  • The deserialized Project.

Param Description
baseDir

The base directory to read the project from.

opts

An options object.

opts.linkDeps

Include linking dependencies from the Project's node_modules.

opts.linkDevDeps

Include linking devDependencies from the Project's node_modules.

changelog (log de mudanças)

Changelog

Release (2024-10-29)

fixturify-project 7.1.3 (patch)

:house: Internal

  • fixturify-project
    • #90 Move to release-plan for releases (@mansona)
    • #102 test both cjs and esm versions (and fix the esm version) (@mansona)

Committers: 1

v7.1.2 (2024-06-25)

:bug: Bug Fix

Committers: 2

v7.1.1 (2024-05-02)

:rocket: Enhancement

  • #94 Publish with sourcemaps (@ef4)

:bug: Bug Fix

  • #95 Handle "transitive peer deps" correctly when hardlinking dependencies (@ef4)

Committers: 1

  • Edward Faulkner (@ef4)

v7.1.0 (2024-05-01)

:rocket: Enhancement

  • #93 Apply the "pnpm pack" rules when hard linking in-monorepo deps (@ef4)

:bug: Bug Fix

  • #92 fs-extra is a dep not a dev-dep (@ef4)

Committers: 1

  • Edward Faulkner (@ef4)

v7.0.0 (2024-04-09)

:boom: Breaking Change

  • #87 Closes #86 Files leak across instances of Project (@lolmaus). This is marked as breaking because it changes the merging behavior of the files argument to new Project to do deep merging when it didn't before.

:house: Internal

Committers: 2

v6.0.0 (2023-08-03)

:boom: Breaking Change

  • #85 Bump Node to 16, drop deprecated methods (@lolmaus)

:bug: Bug Fix

  • #82 throw if no version is specified in dependency's package.json (@cafreeman)

:house: Internal

  • #83 Maintenance: bump Node, typescript and type-fest (@lolmaus)

Committers: 4

v5.2.0 (2022-10-26)

:rocket: Enhancement

  • #76 provide access to baseDir before write (@ef4)

Committers: 1

  • Edward Faulkner (@ef4)

v5.1.0 (2022-09-15)

:rocket: Enhancement

  • #73 allow linking to other Projects (@ef4)

Committers: 1

  • Edward Faulkner (@ef4)

v5.0.3 (2022-05-21)

:bug: Bug Fix

  • #70 Fix transitive peer dependencies (@ef4)

Committers: 1

  • Edward Faulkner (@ef4)

v5.0.2 (2022-04-18)

:bug: Bug Fix

:memo: Documentation

Committers: 2

v5.0.1 (2022-04-15)

:boom: Breaking Change

  • #62 Converts to multi-target publishing (cjs/esm) w/ declaration files. (@scalvert)

:rocket: Enhancement

:memo: Documentation

  • #66 Add readme-api-generator to auto-generate API documentation for README (@scalvert)

Committers: 1

v4.1.2 (2022-04-12)

:bug: Bug Fix

  • #59 Fixes typeguard to correctly evaluate Error type when globals don't match (@scalvert)

:house: Internal

Committers: 1

4.0.2

  • [BUGFIX] Fix odd double project directory structure when resolving linked deps

4.0.1

  • [BUGFIX] some cross-device scenarios were broken by last minute changes in the previous release, this fixes them.

4.0.0

  • [ENHANCEMENT] We now ensure that dependencies linked into a project always see the correct peerDependencies.
  • [BREAKING] the linkDeps argument originally linked both dependencies and devDependencies. In the course of implementing correct peerDep support, it was discovered that linking devDependencies is not typically desirable, so linkDeps has switched to only link dependencies, and you should pass linkDevDeps to explicitly opt in to lining devDependencies.

3.0.2

  • [BUGFIX] fix CB API

3.0.1

  • restore CB API
  • improve changelog

3.0.0

  • Drop legacy node support
  • Support Dependency Linking
  • [Breaking] project.root has been removed you can now use project.baseDir;
  • Add Changelog for moving forward
  • [Breaking] the project is now required via const { Project } = require('fixturify-project')