Skip to content

Commit

Permalink
Merge pull request plopjs#75 from amwmedia/crutchcorn-binary-copy
Browse files Browse the repository at this point in the history
Fixes to binary copy logic
  • Loading branch information
amwmedia authored Apr 18, 2018
2 parents 28ff04a + 2b6f93c commit 784c3cd
Show file tree
Hide file tree
Showing 10 changed files with 509 additions and 431 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["es2015-node4"]
"presets": ["node8"]
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-plop",
"version": "0.13.0",
"version": "0.14.0",
"description": "programmatic plopping for fun and profit",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -42,8 +42,7 @@
"devDependencies": {
"ava": "^0.25.0",
"babel-cli": "^6.16.0",
"babel-preset-es2015-node4": "^2.1.0",
"babel-register": "^6.18.0",
"babel-preset-node8": "^1.2.0",
"eslint": "^4.7.2",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.8.0",
Expand All @@ -63,6 +62,7 @@
"globby": "^8.0.0",
"handlebars": "^4.0.5",
"inquirer": "^5.0.0",
"isbinaryfile": "^3.0.2",
"lodash.get": "^4.4.2",
"mkdirp": "^0.5.1",
"pify": "^3.0.0",
Expand Down
23 changes: 17 additions & 6 deletions src/actions/_common-action-add-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,40 @@ import {
throwStringifiedError,
getRelativeToBasePath
} from './_common-action-utils';
import isBinary from 'isbinaryfile';
import * as fspp from '../fs-promise-proxy';

export default function* addFile(data, cfg, plop) {
const fileDestPath = makeDestPath(data, cfg, plop);
const { force, skipIfExists = false } = cfg;
try {
// check path
let pathExists = yield fspp.fileExists(fileDestPath);
let destExists = yield fspp.fileExists(fileDestPath);

// if we are forcing and the file already exists, delete the file
if (force === true && pathExists) {
if (force === true && destExists) {
yield del([fileDestPath]);
pathExists = false;
destExists = false;
}

// we can't create files where one already exists
if (pathExists) {
if (destExists) {
if (skipIfExists) { return `[SKIPPED] ${fileDestPath} (exists)`; }
throw `File already exists\n -> ${fileDestPath}`;
} else {
yield fspp.makeDir(path.dirname(fileDestPath));
const renderedTemplate = yield getRenderedTemplate(data, cfg, plop);
yield fspp.writeFile(fileDestPath, renderedTemplate);

const absTemplatePath = cfg.templateFile
&& path.resolve(plop.getPlopfilePath(), cfg.templateFile)
|| null;

if (absTemplatePath != null && isBinary.sync(absTemplatePath)) {
const rawTemplate = yield fspp.readFileRaw(cfg.templateFile);
yield fspp.writeFileRaw(fileDestPath, rawTemplate);
} else {
const renderedTemplate = yield getRenderedTemplate(data, cfg, plop);
yield fspp.writeFile(fileDestPath, renderedTemplate);
}
}

// return the added file path (relative to the destination path)
Expand Down
17 changes: 11 additions & 6 deletions src/actions/addMany.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import co from 'co';
import path from 'path';
import fs from 'fs';
import {readFile} from '../fs-promise-proxy';
import globby from 'globby';
import isBinary from 'isbinaryfile';
import {readFile} from '../fs-promise-proxy';
import actionInterfaceTest from './_common-action-interface-check';
import addFile from './_common-action-add-file';

Expand All @@ -18,24 +19,28 @@ export default co.wrap(function* (data, cfg, plop) {
cfg.base = plop.renderString(cfg.base, data);
}

if(typeof cfg.templateFiles === 'function'){
if (typeof cfg.templateFiles === 'function'){
cfg.templateFiles = cfg.templateFiles();
}

cfg.templateFiles = []
.concat(cfg.templateFiles) // Ensure `cfg.templateFiles` is an array, even if a string is passed.
.map((file) => plop.renderString(file, data)); // render the paths as hbs templates

const templateFiles = resolveTemplateFiles(cfg.templateFiles, cfg.base, cfg.globOptions, plop);

const filesAdded = [];
for (let templateFile of templateFiles) {
const absTemplatePath = path.resolve(plop.getPlopfilePath(), templateFile);
const fileCfg = Object.assign({}, cfg, {
path: resolvePath(cfg.destination, templateFile, cfg.base),
template: yield readFile(path.resolve(plop.getPlopfilePath(), templateFile))
path: resolvePath(cfg.destination, templateFile, cfg.base)
});
Object.assign(fileCfg, (
isBinary.sync(absTemplatePath)
? { templateFile: absTemplatePath }
: { template: yield readFile(absTemplatePath) }
));
const addedPath = yield addFile(data, fileCfg, plop);

filesAdded.push(addedPath);
}

Expand Down
2 changes: 2 additions & 0 deletions src/fs-promise-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ export const makeDir = pify(mkdirp);
export const readdir = pify(fs.readdir);
export const readFile = path => _readFile(path, 'utf8');
export const writeFile = (path, data) => _writeFile(path, data, 'utf8');
export const readFileRaw = path => _readFile(path, null);
export const writeFileRaw = (path, data) => _writeFile(path, data, null);
export const fileExists = path => _access(path).then(() => true, () => false);
Binary file added tests/add-action-binary-file-mock/plop-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions tests/add-action-binary-file.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'fs';
import co from 'co';
import path from 'path';
import AvaTest from './_base-ava-test';
const {test, mockPath, testSrcPath, nodePlop} = (new AvaTest(__filename));

const plop = nodePlop();

/////
//
//

test('Add action does not fail on binary file', co.wrap(function* (t) {
plop.setGenerator('addBinary', {
actions: [{
type: 'add',
path: `${testSrcPath}/{{dashCase name}}-plop-logo.png`,
templateFile: `${mockPath}/plop-logo.png`
}]
});

const filePath = path.resolve(testSrcPath, 'test-plop-logo.png');
yield plop.getGenerator('addBinary').runActions({name: 'test'});
t.true(fs.existsSync(filePath));
}));
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion tests/addMany-multiple-files.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ test('Test the content of the rendered file in nested folder', t => {
test('Test the base value is used to decide which files are created', t => {
const expectedCreatedFiles = [
'components/john-doe-ctrl.js',
'components/john-doe-tmpl.html'
'components/john-doe-tmpl.html',
'components/john-doe-plop-logo.png'
];
expectedCreatedFiles.map((file) => {
const filePath = path.resolve(testSrcPath, file);
Expand Down
Loading

0 comments on commit 784c3cd

Please sign in to comment.