|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import tar from 'tar'; |
| 4 | +import os from 'os'; |
| 5 | + |
| 6 | +import handleActions from '../../actions'; |
| 7 | +import getLogger from '../../utils/getLogger'; |
| 8 | +import choosePart from '../start/choosePart'; |
| 9 | +import getParser from '../start/fetchBluprint/parser'; |
| 10 | +import minimatch from 'minimatch'; |
| 11 | + |
| 12 | +const logger = getLogger(); |
| 13 | + |
| 14 | +// best effort attempt at reproducing .gitignore behaviour |
| 15 | +const createIgnoreFilter = config => { |
| 16 | + const globs = fs.readFileSync(config, 'utf-8').split('\n').filter(line => !line.trim().startsWith('#')); |
| 17 | + return file => !globs.some(glob => file.startsWith(glob) || minimatch(file, glob)); |
| 18 | +}; |
| 19 | + |
| 20 | +const getFiles = dir => { |
| 21 | + const basename = path.basename(dir); |
| 22 | + const files = fs.readdirSync(dir); |
| 23 | + const result = []; |
| 24 | + |
| 25 | + let filterFn = () => true; |
| 26 | + |
| 27 | + for (const file of files) { |
| 28 | + const fullPath = path.join(dir, file); |
| 29 | + |
| 30 | + if (file === '.gitignore') { |
| 31 | + filterFn = createIgnoreFilter(fullPath); |
| 32 | + } |
| 33 | + |
| 34 | + if (fs.statSync(fullPath).isDirectory()) { |
| 35 | + getFiles(fullPath).forEach(f => result.push(f)); |
| 36 | + } else { |
| 37 | + result.push(file); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return result.filter(e => filterFn(e) && !e.match(/^\.git(\/|$)/)).map(e => [basename, path.sep, e].join('')); |
| 42 | +}; |
| 43 | + |
| 44 | +const buildTarball = async(directory) => { |
| 45 | + const tarball = path.join(os.tmpdir(), 'tmp-bluprint.tar'); |
| 46 | + const files = getFiles(directory); |
| 47 | + |
| 48 | + await tar.create({ |
| 49 | + gzip: false, |
| 50 | + file: tarball, |
| 51 | + cwd: path.dirname(directory), |
| 52 | + strict: true, |
| 53 | + }, files); |
| 54 | + |
| 55 | + return tarball; |
| 56 | +}; |
| 57 | + |
| 58 | +const fetchBluprint = async(tarballPath, filterGlobs, mergeJson) => { |
| 59 | + return new Promise((resolve, reject) => { |
| 60 | + const rs = fs.createReadStream(tarballPath); |
| 61 | + const parser = getParser(resolve, reject, filterGlobs, mergeJson); |
| 62 | + |
| 63 | + rs.pipe(parser) |
| 64 | + .on('error', (e) => { |
| 65 | + logger.error(`Tarball parsing error.`); |
| 66 | + reject(e); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}; |
| 70 | + |
| 71 | +const defaultInject = { |
| 72 | + method: null, |
| 73 | + category: null, |
| 74 | + bluprint: null, |
| 75 | + partConfirm: null, |
| 76 | + partChoice: null, |
| 77 | +}; |
| 78 | + |
| 79 | +export default async(directory, inject = defaultInject) => { |
| 80 | + const bluprintrc = JSON.parse(fs.readFileSync(path.join(directory, '.bluprintrc'))); |
| 81 | + |
| 82 | + const { parts, mergeJson } = bluprintrc; |
| 83 | + const { part, globs: filterGlobs } = await choosePart(parts, inject); |
| 84 | + |
| 85 | + const tarball = await buildTarball(directory); |
| 86 | + |
| 87 | + try { |
| 88 | + await fetchBluprint(tarball, filterGlobs, mergeJson); |
| 89 | + await handleActions(bluprintrc.actions, part); |
| 90 | + } finally { |
| 91 | + fs.rmSync(tarball); |
| 92 | + } |
| 93 | +}; |
0 commit comments