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 projectproject.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 itexpect(fs.existsSync(join(project.baseDir, 'index.js'))).to.eql(true);
The above example produces the following files (and most importantly the
appropriate file contents:
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();
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 targetproject.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();
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.