scss-bundle
Advanced tools
Comparing version 0.1.2 to 0.2.0
#!/usr/bin/env node | ||
"use strict"; | ||
var fs = require('fs'); | ||
var optimist = require('optimist'); | ||
var bundle_1 = require('./bundle'); | ||
var path = require('path'); | ||
var Cli = (function () { | ||
function Cli(args) { | ||
var _this = this; | ||
this.package = {}; | ||
var packageJSONPath = path.join(__dirname, '../package.json'); | ||
this.package = JSON.parse(fs.readFileSync(packageJSONPath, 'utf8')); | ||
var argv = args.argv; | ||
if (argv.help) { | ||
this.printVersion(); | ||
console.info(args.help()); | ||
} | ||
else if (argv.version) { | ||
this.printVersion(); | ||
} | ||
else { | ||
if (argv.config == null) { | ||
args.demand(['e', 'd']); | ||
argv = args.argv; | ||
this.config = { | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments)).next()); | ||
}); | ||
}; | ||
const fs = require('fs'); | ||
const bundle_1 = require('./bundle'); | ||
const arguments_1 = require('./arguments'); | ||
const path = require('path'); | ||
const DEFAULT_CONFIG_NAME = 'scss-bundle.config.json'; | ||
class Cli { | ||
constructor(argv) { | ||
let configFileName = argv.config || DEFAULT_CONFIG_NAME; | ||
this.main(configFileName, argv); | ||
} | ||
main(configFileName, argv) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let fullPath = path.join(process.cwd(), configFileName); | ||
let configExists = yield this.checkConfigIsExist(fullPath); | ||
if (argv.dest != null && argv.entry != null && argv.config == null) { | ||
this.bundle({ | ||
entry: argv.entry, | ||
dest: argv.dest | ||
}; | ||
}); | ||
} | ||
else if ((argv.dest == null || argv.entry == null) && argv.config == null) { | ||
this.throwError('[Error] `Dest` or `Entry` argument is missing.'); | ||
} | ||
else if (configExists) { | ||
let config = yield this.readConfigFile(configFileName).catch((err) => { | ||
this.throwError(`[Error] Config file ${configFileName} is not valid.`); | ||
}); | ||
console.info('Using config:', fullPath); | ||
this.bundle(this.getConfig(config, argv)); | ||
} | ||
else { | ||
this.config = this.getConfig(argv.config); | ||
this.throwError(`[Error] Config file ${configFileName} was not found.`); | ||
} | ||
new bundle_1.default(this.config) | ||
.Bundle() | ||
.then(function () { | ||
console.info("[Done] Bundling done. Destination: " + _this.config.dest + "."); | ||
}) | ||
.catch(function (error) { | ||
console.error("[Error] Bundling done with errors. " + error); | ||
process.exit(1); | ||
}); | ||
} | ||
bundle(config) { | ||
new bundle_1.default(config) | ||
.Bundle() | ||
.then(() => { | ||
console.info(`[Done] Bundling done. Destination: ${config.dest}.`); | ||
}) | ||
.catch((error) => { | ||
this.throwError(`[Error] Bundling done with errors. ${error}`); | ||
}); | ||
} | ||
getConfig(config, argv) { | ||
if (argv.entry != null) | ||
config.entry = argv.entry; | ||
if (argv.dest != null) | ||
config.dest = argv.dest; | ||
return config; | ||
} | ||
checkConfigIsExist(fullPath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve, reject) => { | ||
fs.access(fullPath, fs.F_OK, (err) => __awaiter(this, void 0, void 0, function* () { | ||
if (!err) { | ||
resolve(true); | ||
} | ||
else { | ||
resolve(false); | ||
} | ||
})); | ||
}); | ||
} | ||
}); | ||
} | ||
Cli.prototype.getConfig = function (filePath) { | ||
var fullPath = path.join(process.cwd(), filePath); | ||
try { | ||
fs.accessSync(fullPath, fs.F_OK); | ||
return JSON.parse(fs.readFileSync(fullPath, 'utf8')); | ||
} | ||
catch (e) { | ||
if (typeof filePath === 'boolean') { | ||
console.error("[Error] Config file path is not set."); | ||
} | ||
else { | ||
console.error("[Error] Config " + filePath + " was not found."); | ||
} | ||
process.exit(1); | ||
} | ||
}; | ||
Cli.prototype.printVersion = function () { | ||
console.info("Version " + this.package['version'] + " \n"); | ||
}; | ||
return Cli; | ||
}()); | ||
var argv = optimist | ||
.options('h', { | ||
alias: 'help', | ||
describe: 'Prints this message.' | ||
}) | ||
.options('v', { | ||
alias: 'version', | ||
describe: 'Prints version.' | ||
}) | ||
.options('c', { | ||
alias: 'config', | ||
describe: 'Config file path.' | ||
}) | ||
.options('e', { | ||
alias: 'entry', | ||
describe: 'Entry file.' | ||
}) | ||
.options('d', { | ||
alias: 'dest', | ||
describe: 'Bundled file destination.' | ||
}) | ||
.usage('Usage: scss-bundle [options]') | ||
.boolean(['h', 'v']) | ||
.string(['c', 'e', 'd']); | ||
var commandLine = new Cli(argv); | ||
readConfigFile(fullPath) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(fullPath, 'utf8', (err, data) => { | ||
if (!err) { | ||
let configData; | ||
try { | ||
configData = JSON.parse(data); | ||
resolve(configData); | ||
} | ||
catch (e) { | ||
reject(e); | ||
} | ||
} | ||
else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
throwError(message) { | ||
console.error(message); | ||
process.exit(1); | ||
} | ||
} | ||
new Cli(arguments_1.default); |
@@ -1,12 +0,11 @@ | ||
"use strict"; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Helpers = require('./helpers'); | ||
var sass = require('node-sass'); | ||
var Promise = require('promise'); | ||
var IMPORT_PATTERN = /@import '(.+)';/g; | ||
var COMMENTED_IMPORT_PATTERN = /\/\/@import '(.+)';/g; | ||
var FILE_EXTENSION = '.scss'; | ||
var Bundle = (function () { | ||
function Bundle(config) { | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const Helpers = require('./helpers'); | ||
const sass = require('node-sass'); | ||
const Promise = require('promise'); | ||
const IMPORT_PATTERN = /@import '(.+)';/g; | ||
const COMMENTED_IMPORT_PATTERN = /\/\/@import '(.+)';/g; | ||
const FILE_EXTENSION = '.scss'; | ||
class Bundle { | ||
constructor(config) { | ||
this.dependencies = {}; | ||
@@ -18,8 +17,7 @@ this.files = Array(); | ||
} | ||
Bundle.prototype.Bundle = function () { | ||
var _this = this; | ||
Bundle() { | ||
while (this.files.length > 0) { | ||
var file = this.files.shift(); | ||
var imports = this.getImports(file); | ||
(_a = this.files).unshift.apply(_a, imports); | ||
let file = this.files.shift(); | ||
let imports = this.getImports(file); | ||
this.files.unshift(...imports); | ||
if (this.dependencies[file] == null) { | ||
@@ -29,41 +27,39 @@ this.dependencies[file] = this.getFileContents(file); | ||
} | ||
var bundledFileContents = this.bundling(this.entry_file); | ||
return new Promise(function (resolve, reject) { | ||
sass.render({ data: bundledFileContents }, function (error, result) { | ||
let bundledFileContents = this.bundling(this.entry_file); | ||
return new Promise((resolve, reject) => { | ||
sass.render({ data: bundledFileContents }, (error, result) => { | ||
if (error != null) { | ||
reject(error.message + " at line " + error.line + "."); | ||
reject(`${error.message} at line ${error.line}.`); | ||
} | ||
else { | ||
resolve(null); | ||
resolve({}); | ||
} | ||
_this.writeToFile(bundledFileContents); | ||
this.writeToFile(bundledFileContents); | ||
}); | ||
}); | ||
var _a; | ||
}; | ||
Bundle.prototype.writeToFile = function (contents) { | ||
} | ||
writeToFile(contents) { | ||
fs.writeFileSync(this.config.dest, contents); | ||
}; | ||
Bundle.prototype.bundling = function (file_path) { | ||
var _this = this; | ||
} | ||
bundling(file_path) { | ||
if (this.dependencies[file_path] == null) { | ||
return undefined; | ||
} | ||
var content = this.removeCommentedImports(this.dependencies[file_path]); | ||
let content = this.removeCommentedImports(this.dependencies[file_path]); | ||
delete this.dependencies[file_path]; | ||
var folder_path = path.dirname(file_path); | ||
return this.replaceImports(content, function (matches) { | ||
var file = matches[1]; | ||
let folder_path = path.dirname(file_path); | ||
return this.replaceImports(content, (matches) => { | ||
let file = matches[1]; | ||
if (file.indexOf(FILE_EXTENSION) === -1) { | ||
file += FILE_EXTENSION; | ||
} | ||
var fullPath = path.join(folder_path, file); | ||
var content = _this.bundling(fullPath) || ''; | ||
let fullPath = path.join(folder_path, file); | ||
let content = this.bundling(fullPath) || ''; | ||
content += '\n'; | ||
return content; | ||
}); | ||
}; | ||
Bundle.prototype.replaceImports = function (content, callback) { | ||
var imports = Helpers.getMatches(content, IMPORT_PATTERN, -1); | ||
for (var i = 0; i < imports.length; i++) { | ||
} | ||
replaceImports(content, callback) { | ||
let imports = Helpers.getMatches(content, IMPORT_PATTERN, -1); | ||
for (let i = 0; i < imports.length; i++) { | ||
if (imports[i].length > 0) { | ||
@@ -74,12 +70,11 @@ content = content.replace(imports[i][0], callback(imports[i])); | ||
return content; | ||
}; | ||
Bundle.prototype.getImports = function (file_path, fullPaths) { | ||
if (fullPaths === void 0) { fullPaths = true; } | ||
var paths = Array(); | ||
var file_folder = path.dirname(file_path); | ||
var content = this.getFileContents(file_path); | ||
} | ||
getImports(file_path, fullPaths = true) { | ||
let paths = Array(); | ||
let file_folder = path.dirname(file_path); | ||
let content = this.getFileContents(file_path); | ||
content = this.removeCommentedImports(content); | ||
var imports = Helpers.getMatches(content, IMPORT_PATTERN); | ||
for (var key in imports) { | ||
var import_file_path = fullPaths ? path.join(file_folder, imports[key]) : imports[key]; | ||
let imports = Helpers.getMatches(content, IMPORT_PATTERN); | ||
for (let key in imports) { | ||
let import_file_path = fullPaths ? path.join(file_folder, imports[key]) : imports[key]; | ||
if (import_file_path.indexOf(FILE_EXTENSION) === -1) { | ||
@@ -91,12 +86,11 @@ import_file_path += FILE_EXTENSION; | ||
return paths; | ||
}; | ||
Bundle.prototype.removeCommentedImports = function (content) { | ||
} | ||
removeCommentedImports(content) { | ||
return content.replace(COMMENTED_IMPORT_PATTERN, ''); | ||
}; | ||
Bundle.prototype.getFileContents = function (file_path) { | ||
} | ||
getFileContents(file_path) { | ||
return fs.readFileSync(file_path, 'utf8').toString(); | ||
}; | ||
return Bundle; | ||
}()); | ||
} | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Bundle; |
@@ -1,6 +0,4 @@ | ||
"use strict"; | ||
function getMatches(string, regex, index) { | ||
if (index === void 0) { index = 1; } | ||
var matches = new Array(); | ||
var match; | ||
function getMatches(string, regex, index = 1) { | ||
let matches = new Array(); | ||
let match; | ||
while (match = regex.exec(string)) { | ||
@@ -7,0 +5,0 @@ if (index !== -1) { |
{ | ||
"name": "scss-bundle", | ||
"version": "0.1.2", | ||
"description": "Bundling SCSS files to one file bundle.", | ||
"version": "0.2.0", | ||
"description": "Bundling SCSS files to one bundled file.", | ||
"main": "index.js", | ||
@@ -21,4 +21,7 @@ "scripts": { | ||
"devDependencies": { | ||
"typescript": "1.8.10", | ||
"typings": "^1.3.1" | ||
"@types/node": "^6.0.32", | ||
"@types/node-sass": "0.0.28", | ||
"@types/promise": "^7.1.27", | ||
"@types/yargs": "0.0.28", | ||
"typescript": "1.8.10" | ||
}, | ||
@@ -29,7 +32,6 @@ "bin": { | ||
"dependencies": { | ||
"minimist": "^1.2.0", | ||
"node-sass": "^3.8.0", | ||
"optimist": "^0.6.1", | ||
"promise": "^7.1.1" | ||
"promise": "^7.1.1", | ||
"yargs": "^4.8.1" | ||
} | ||
} |
scss-bundle | ||
=========== | ||
[![NPM version](http://img.shields.io/npm/v/scss-bundle.svg)](https://www.npmjs.com/package/scss-bundle) [![dependencies Status](https://david-dm.org/quatrocode/scss-bundle/status.svg)](https://david-dm.org/quatrocode/scss-bundle) [![devDependencies Status](https://david-dm.org/quatrocode/scss-bundle/dev-status.svg)](https://david-dm.org/quatrocode/scss-bundle?type=dev) | ||
@@ -45,2 +46,2 @@ ## Get started | ||
## License | ||
Released under the [PGL-3.0 license](LICENSE). | ||
Released under the [PGL-3.0 license](LICENSE). |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
3
47
45550
5
10
245
1
+ Addedyargs@^4.8.1
+ Addedwindow-size@0.2.0(transitive)
+ Addedyargs@4.8.1(transitive)
+ Addedyargs-parser@2.4.1(transitive)
- Removedminimist@^1.2.0
- Removedoptimist@^0.6.1
- Removedminimist@0.0.10(transitive)
- Removedoptimist@0.6.1(transitive)
- Removedwordwrap@0.0.3(transitive)