Skip to content

Commit

Permalink
Feat: update to rcs-core@2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
JPeer264 committed Jun 8, 2018
1 parent 63001eb commit f1e5e56
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 14 deletions.
32 changes: 32 additions & 0 deletions lib/helper/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('fs-extra');
const path = require('path');

const save = (destinationPath, data, opts, cb) => {
// @todo check if the filepath has an .ext
let callback = cb;
let options = opts;

// set cb if options are not set
if (typeof options === 'function') {
callback = options;
options = {};
}

if (!options.overwrite && fs.existsSync(destinationPath)) {
return callback({
message: 'File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.',
});
}

return fs.mkdirs(path.dirname(destinationPath), () => {
fs.writeFile(destinationPath, data, (err) => {
if (err) {
return callback(err);
}

return callback(null, `Successfully wrote ${destinationPath}`);
});
});
}; // /save

module.exports = save;
17 changes: 17 additions & 0 deletions lib/helper/saveSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs-extra');
const path = require('path');

const saveSync = (destinationPath, data, options = {}) => {
if (!options.overwrite && fs.existsSync(destinationPath)) {
throw new Error('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.');
}

try {
fs.mkdirsSync(path.dirname(destinationPath));
fs.writeFileSync(destinationPath, data);
} catch (err) {
throw err;
}
}; // /saveSync

module.exports = saveSync;
38 changes: 38 additions & 0 deletions lib/helper/tests/save.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require('fs-extra');
const path = require('path');
const expect = require('chai').expect;

const save = require('../save');

const testCwd = 'test/files/testCache';

describe('save', () => {
afterEach(() => {
fs.removeSync(testCwd);
});

it('should create a file within a non existing dir', (done) => {
const filePath = path.join(testCwd, '/a/non/existing/path/test.txt');

save(filePath, 'test content', (err) => {
expect(err).not.to.equal(undefined);

expect(fs.existsSync(filePath)).to.equal(true);
expect(fs.readFileSync(filePath, 'utf8')).to.equal('test content');

done();
});
});

it('should not overwrite the same file', (done) => {
const filePath = path.join(testCwd, '/../config.json');
const oldFile = fs.readFileSync(filePath, 'utf8');

save(filePath, 'test content', (err) => {
expect(err.message).to.equal('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.');
expect(fs.readFileSync(filePath, 'utf8')).to.equal(oldFile);

done();
});
});
});
39 changes: 39 additions & 0 deletions lib/helper/tests/saveSync.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const fs = require('fs-extra');
const path = require('path');
const expect = require('chai').expect;

const saveSync = require('../saveSync');

const testCwd = 'test/files/testCache';

describe('saveSync', () => {
afterEach(() => {
fs.removeSync(testCwd);
});

it('saveSync | should save', () => {
const filePath = path.join(testCwd, '/config.txt');

saveSync(filePath, 'test content');

expect(fs.readFileSync(filePath, 'utf8')).to.equal('test content');
});

it('saveSync | should not overwrite the same file', () => {
const filePath = path.join(testCwd, '/../config.json');
const oldFile = fs.readFileSync(filePath, 'utf8');
let failed = false;

try {
saveSync(filePath, 'test content');

// if no error is thrown before it should fail here
failed = true;
} catch (e) {
expect(e.message).to.equal('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.');
}

expect(failed).to.equal(false);
expect(fs.readFileSync(filePath, 'utf8')).to.equal(oldFile);
});
});
4 changes: 3 additions & 1 deletion lib/mapping/generateMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const rcs = require('rcs-core');
const path = require('path');
const json = require('json-extra');

const save = require('../helper/save');

/**
* @typedef {Object} generateMappingOptions
* @property {Boolean | String} [cssMapping=true] true will generate the css mapping. A string will generate the css mapping file and the object is called like the string
Expand Down Expand Up @@ -74,7 +76,7 @@ const generateMapping = (pathString, options, cb) => {
fileNameExt = '.js';
}

rcs.helper.save(`${newPath}${fileNameExt}`, writeData, { overwrite: options.overwrite }, (err, data) => {
save(`${newPath}${fileNameExt}`, writeData, { overwrite: options.overwrite }, (err, data) => {
if (err) cb(err);

cb(null, data);
Expand Down
4 changes: 3 additions & 1 deletion lib/mapping/generateMappingSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const rcs = require('rcs-core');
const path = require('path');
const json = require('json-extra');

const saveSync = require('../helper/saveSync');

/**
* The synchronous method of generateMapping
*/
Expand Down Expand Up @@ -56,7 +58,7 @@ const generateMappingSync = (pathString, options) => {
fileNameExt = '.js';
}

rcs.helper.saveSync(`${newPath}${fileNameExt}`, writeData, { overwrite: options.overwrite });
saveSync(`${newPath}${fileNameExt}`, writeData, { overwrite: options.overwrite });
}; // /generateMappingSync

module.exports = generateMappingSync;
6 changes: 4 additions & 2 deletions lib/process/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const path = require('path');
const glob = require('glob');
const async = require('async');

const save = require('../helper/save');

/**
* @typedef {Object} processOptions
* @property {Boolean} [collectSelectors=false] if the triggered files are css files
Expand Down Expand Up @@ -90,7 +92,7 @@ const rcsProcess = (pathString, options, cb) => {
shouldOverwrite = joinedPath !== path.join(options.cwd, filePath);
}

return rcs.helper.save(joinedPath, data, { overwrite: shouldOverwrite }, (err) => {
return save(joinedPath, data, { overwrite: shouldOverwrite }, (err) => {
if (err) return callback(err);

return callback();
Expand Down Expand Up @@ -127,7 +129,7 @@ const rcsProcess = (pathString, options, cb) => {
shouldOverwrite = joinedPath !== path.join(options.cwd, filePath);
}

return rcs.helper.save(joinedPath, data, { overwrite: shouldOverwrite }, (err) => {
return save(joinedPath, data, { overwrite: shouldOverwrite }, (err) => {
if (err) return callback(err);

return callback();
Expand Down
4 changes: 3 additions & 1 deletion lib/process/processSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const rcs = require('rcs-core');
const path = require('path');
const glob = require('glob');

const saveSync = require('../helper/saveSync');

/**
* The synchronous method for process
*/
Expand Down Expand Up @@ -60,7 +62,7 @@ const processSync = (pathString, options) => {
data = rcs.replace.any(fileData);
}

rcs.helper.saveSync(joinedPath, data, { overwrite: shouldOverwrite });
saveSync(joinedPath, data, { overwrite: shouldOverwrite });
});
}; // /processSync

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"glob": "^7.1.1",
"json-extra": "^0.5.0",
"lodash": "^4.17.4",
"rcs-core": "1.0.5"
"rcs-core": "^2.0.1"
},
"devDependencies": {
"chai": "^3.5.0",
Expand Down
4 changes: 2 additions & 2 deletions test/files/results/css/css-attributes-pre-suffix.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.prefix-selector-suffix[class*="block"] {
.prefix-tctor-suffix[class*="block"] {
}

.prefix-block-suffix[class*="block"] {
.prefix-tblockn-suffix[class*="block"] {
}

.prefix-a-suffix[id^="prefix-pre-"] {
Expand Down
4 changes: 2 additions & 2 deletions test/files/results/css/css-attributes.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.selector[class*="block"] {
.tctor[class*="block"] {
}

.block[class*="block"] {
.tblockn[class*="block"] {
}

.a[id^="pre-"] {
Expand Down
12 changes: 8 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,10 @@ lodash.keys@^3.0.0:
lodash.isarguments "^3.0.0"
lodash.isarray "^3.0.0"

lodash.merge@^4.6.1:
version "4.6.1"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.1.tgz#adc25d9cb99b9391c59624f379fbba60d7111d54"

lodash@^4.0.0, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.3.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
Expand Down Expand Up @@ -1448,17 +1452,17 @@ qs@~6.2.0:
version "6.2.1"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.2.1.tgz#ce03c5ff0935bc1d9d69a9f14cbd18e568d67625"

rcs-core@1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/rcs-core/-/rcs-core-1.0.5.tgz#6ea3dad913141a6d7388b72ea2a94df1a19c2f05"
rcs-core@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/rcs-core/-/rcs-core-2.0.1.tgz#b4ec1b9b5e3a3919397ca0dfc68779d22a358e75"
dependencies:
array-includes "^3.0.2"
ast-traverse "^0.1.1"
decimal-to-any "1.0.0"
espree "^3.4.3"
fs-extra "^2.0.0"
json-extra "^0.5.0"
lodash "^4.17.4"
lodash.merge "^4.6.1"
object.entries "^1.0.4"
postcss "^6.0.17"
recast "^0.12.5"
Expand Down

0 comments on commit f1e5e56

Please sign in to comment.