-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to test a local bluprint from disk
- Loading branch information
Showing
6 changed files
with
183 additions
and
3 deletions.
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 |
---|---|---|
|
@@ -152,4 +152,6 @@ tmp/ | |
temp/ | ||
|
||
# End of https://www.gitignore.io/api/node,macos,linux | ||
n | ||
|
||
# Editor | ||
.vscode |
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
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,93 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import tar from 'tar'; | ||
import os from 'os'; | ||
|
||
import handleActions from '../../actions'; | ||
import getLogger from '../../utils/getLogger'; | ||
import choosePart from '../start/choosePart'; | ||
import getParser from '../start/fetchBluprint/parser'; | ||
import minimatch from 'minimatch'; | ||
|
||
const logger = getLogger(); | ||
|
||
// best effort attempt at reproducing .gitignore behaviour | ||
const createIgnoreFilter = config => { | ||
const globs = fs.readFileSync(config, 'utf-8').split('\n').filter(line => !line.trim().startsWith('#')); | ||
return file => !globs.some(glob => file.startsWith(glob) || minimatch(file, glob)); | ||
}; | ||
|
||
const getFiles = dir => { | ||
const basename = path.basename(dir); | ||
const files = fs.readdirSync(dir); | ||
const result = []; | ||
|
||
let filterFn = () => true; | ||
|
||
for (const file of files) { | ||
const fullPath = path.join(dir, file); | ||
|
||
if (file === '.gitignore') { | ||
filterFn = createIgnoreFilter(fullPath); | ||
} | ||
|
||
if (fs.statSync(fullPath).isDirectory()) { | ||
getFiles(fullPath).forEach(f => result.push(f)); | ||
} else { | ||
result.push(file); | ||
} | ||
} | ||
|
||
return result.filter(e => filterFn(e) && !e.match(/^\.git(\/|$)/)).map(e => [basename, path.sep, e].join('')); | ||
}; | ||
|
||
const buildTarball = async(directory) => { | ||
const tarball = path.join(os.tmpdir(), 'tmp-bluprint.tar'); | ||
const files = getFiles(directory); | ||
|
||
await tar.create({ | ||
gzip: false, | ||
file: tarball, | ||
cwd: path.dirname(directory), | ||
strict: true, | ||
}, files); | ||
|
||
return tarball; | ||
}; | ||
|
||
const fetchBluprint = async(tarballPath, filterGlobs, mergeJson) => { | ||
return new Promise((resolve, reject) => { | ||
const rs = fs.createReadStream(tarballPath); | ||
const parser = getParser(resolve, reject, filterGlobs, mergeJson); | ||
|
||
rs.pipe(parser) | ||
.on('error', (e) => { | ||
logger.error(`Tarball parsing error.`); | ||
reject(e); | ||
}); | ||
}); | ||
}; | ||
|
||
const defaultInject = { | ||
method: null, | ||
category: null, | ||
bluprint: null, | ||
partConfirm: null, | ||
partChoice: null, | ||
}; | ||
|
||
export default async(directory, inject = defaultInject) => { | ||
const bluprintrc = JSON.parse(fs.readFileSync(path.join(directory, '.bluprintrc'))); | ||
|
||
const { parts, mergeJson } = bluprintrc; | ||
const { part, globs: filterGlobs } = await choosePart(parts, inject); | ||
|
||
const tarball = await buildTarball(directory); | ||
|
||
try { | ||
await fetchBluprint(tarball, filterGlobs, mergeJson); | ||
await handleActions(bluprintrc.actions, part); | ||
} finally { | ||
fs.rmSync(tarball); | ||
} | ||
}; |
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,73 @@ | ||
const expect = require('expect.js'); | ||
const path = require('path'); | ||
const { test } = require('../dist'); | ||
const os = require('os'); | ||
const fs = require('fs'); | ||
|
||
const resolvePath = (filePath) => path.join(process.cwd(), filePath); | ||
|
||
const createTestBluprint = function() { | ||
const tempdir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-bluprint')); | ||
|
||
fs.writeFileSync(path.join(tempdir, '.bluprintrc'), JSON.stringify({ | ||
bluprint: '^0.0.1', | ||
name: 'My test bluprint', | ||
category: 'testing', | ||
actions: [ | ||
{ | ||
action: 'render', | ||
engine: 'mustache', | ||
context: { foo: 'bar' }, | ||
files: [ | ||
'test-command-1.txt', | ||
'subdir/test-command-2.txt', | ||
], | ||
}, | ||
], | ||
})); | ||
|
||
fs.mkdirSync(path.join(tempdir, 'subdir')); | ||
fs.writeFileSync(path.join(tempdir, 'test-command-1.txt'), 'foo = {{ foo }}'); | ||
fs.writeFileSync(path.join(tempdir, 'subdir', 'test-command-2.txt'), 'foo = {{ foo }}'); | ||
fs.writeFileSync(path.join(tempdir, 'subdir', 'ignored.txt'), 'ignore me'); | ||
fs.writeFileSync(path.join(tempdir, 'subdir', '.gitignore'), '# this should be ignored\nignored.*'); | ||
|
||
return tempdir; | ||
}; | ||
|
||
describe('Test command: test', function() { | ||
this.timeout(10000); | ||
|
||
let testBluprint; | ||
let cleanupFiles; | ||
|
||
beforeEach(async function() { | ||
testBluprint = createTestBluprint(); | ||
|
||
cleanupFiles = [ | ||
testBluprint, | ||
resolvePath('test-command-1.txt'), | ||
resolvePath('subdir/test-command-2.txt'), | ||
resolvePath('subdir/ignored.txt'), | ||
resolvePath('subdir/.gitignore'), | ||
resolvePath('subdir'), | ||
]; | ||
}); | ||
|
||
afterEach(function() { | ||
cleanupFiles.forEach(f => fs.rmSync(f, { recursive: true, force: true })); | ||
}); | ||
|
||
it('Creates a new project from a bluprint directory', async function() { | ||
await test(testBluprint); | ||
|
||
expect(fs.readFileSync('test-command-1.txt', 'utf8')).to.be('foo = bar'); | ||
expect(fs.readFileSync('subdir/test-command-2.txt', 'utf8')).to.be('foo = bar'); | ||
}); | ||
|
||
it('Ignores files from .gitignore', async function() { | ||
await test(testBluprint); | ||
|
||
expect(fs.existsSync('ignored.txt')).to.be(false); | ||
}); | ||
}); |