Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🍕 feat: Adds SB ESLint rules #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "sparkbox"
}
23 changes: 15 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ const readline = require('readline');
const parse = require('./parseSCSS');

module.exports = (params) => {
return new Promise((resolve, reject) => {
let promises = [];
const content = fs.readFileSync(params.base).toString();
const splitter = new Promise((resolve) => {
const promises = [];

const rl = readline.createInterface({
input: fs.createReadStream(params.base)
input: fs.createReadStream(params.base),
});

rl.on('line', (line) => {
Expand All @@ -24,11 +23,19 @@ module.exports = (params) => {

rl.on('close', () => {
Promise.all(promises).then((data) => {
const global = data.map(x => x.css);
const splits = data.map(x => x.splits.join(''));
fs.writeFileSync(params.partial, splits.join(''))
const global = data.map((x) => {
const css = x.css;
return css;
});
const splits = data.map((x) => {
const split = x.splits.join('');
return split;
});
fs.writeFileSync(params.partial, splits.join(''));
resolve(global.join(''));
});
});
})
});

return splitter;
};
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "scss-splinter",
"version": "0.1.0",
"version": "0.2.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha specs/*.js"
"test": "mocha specs/*.js",
"lint": "eslint *.js"
},
"license": "ISC",
"dependencies": {
Expand All @@ -14,6 +15,13 @@
"postcss-nested": "^1.0.0",
"postcss-scss": "^0.4.0"
},
"devDependencies": {
"add": "^2.0.6",
"babel-eslint": "^7.1.1",
"eslint": "^3.11.1",
"eslint-config-sparkbox": "^0.3.0",
"yarn": "^0.17.10"
},
"repository": {
"url": "[email protected]:sparkbox/splinter.git",
"type": "git"
Expand Down
39 changes: 24 additions & 15 deletions parseSCSS.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

const postcss = require('postcss');
const syntax = require('postcss-scss');
const nest = require("postcss-nested");
const nest = require('postcss-nested');

const parser = (css) => {
const splits = [];

/**
* @returns {Function} - PostCSS function
*/
const brandParse = postcss.plugin('brand-parse', (options) => {
return (global, result) => {
global.walkRules(rule => {
rule.walkAtRules(at => {
const brandParse = postcss.plugin('brand-parse', () => {
const result = (global) => {
global.walkRules((rule) => {
rule.walkAtRules((at) => {
if (/brand/.test(at.params)) {
splits.push(`${rule.selector} {`);
splits.push(at.toString());
Expand All @@ -23,7 +23,7 @@ const parser = (css) => {
}
});

rule.walkDecls(decl => {
rule.walkDecls((decl) => {
if (/brand-/.test(decl.value)) {
splits.push(`${rule.selector} { ${decl.toString()}; }`);

Expand All @@ -32,21 +32,30 @@ const parser = (css) => {
});
});
};

return result;
});

/**
* @param {string} css - the css string to parse.
* @returns {Promise} object - containing the global css and the split code
*/
const parseSCSS = (css) => {
//https://github.com/postcss/postcss-nested#options
//use the bubble option to specify mixins to unwrap
const processor = postcss([nest({bubble: ['brand']}), brandParse()]);

return processor.process(css, {syntax})
.then(x => {
return {css: x.css, splits: splits};
});
const parseSCSS = (cssString) => {
/*
* https://github.com/postcss/postcss-nested#options
* use the bubble option to specify mixins to unwrap
*/
const processor = postcss([nest({ bubble: ['brand'] }), brandParse()]);

return processor.process(cssString, { syntax })
.then((x) => {
const result = {
css: x.cssString,
splits,
};

return result;
});
};

return parseSCSS(css);
Expand Down
Loading