Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

spm build should work now

  • Loading branch information...
commit ef9945b7256b9534d0e67b7ba546b0e89a260e4d 1 parent f5082dd
@lepture lepture authored
Showing with 437 additions and 3 deletions.
  1. +27 −0 .gitignore
  2. +40 −1 bin/spm-build
  3. +62 −0 index.js
  4. +294 −0 lib/config.js
  5. +14 −2 package.json
View
27 .gitignore
@@ -0,0 +1,27 @@
+*.iml
+.idea/
+.ipr
+.iws
+*~
+~*
+*.diff
+*.patch
+*.bak
+.DS_Store
+Thumbs.db
+.project
+.*proj
+.svn/
+*.swp
+*.swo
+*.log
+*.sublime-project
+*.sublime-workspace
+node_modules/
+dist/
+tmp/
+.spm-build
+.buildpath
+.settings
+.yml
+_site
View
41 bin/spm-build
@@ -1,3 +1,42 @@
#!/usr/bin/env node
-var build = require('..');
+var commander = require('commander');
+require('colorful').colorful();
+var spm = require('..');
+
+commander.usage('[options]');
+
+commander
+ .option('-I, --input-directory <dir>', 'input directory: [src]')
+ .option('-O, --output-directory <dir>', 'output directory: [dist]')
+ .option('-v, --verbose', 'show more logs')
+ .option('-q, --quiet', 'show less logs')
+ .option('--encoding <encoding>', 'file encoding: [utf8]')
+ .option('--no-color', 'disable colorful print');
+
+
+commander.on('--help', function() {
+ console.log();
+ console.log(' ' + 'Examples:'.to.bold.blue.color);
+ console.log();
+ console.log(' Build a standard cmd module is simple');
+ console.log();
+ console.log(' $ ' + 'spm build'.to.magenta.color);
+ console.log();
+ console.log(' Build from another source');
+ console.log();
+ console.log(' $ ' + 'spm build'.to.magenta.color + ' ' + '-I'.to.cyan.color + ' lib ' + '-O'.to.cyan.color + ' dist');
+ console.log();
+ console.log(' For more information: ' + 'http://docs.spmjs.org/en/build'.to.underline.color);
+ console.log();
+});
+
+
+commander.parse(process.argv);
+
+// verbose vs quiet
+spm.log.config(commander);
+
+// run install
+console.log();
+spm.build(commander);
View
62 index.js
@@ -0,0 +1,62 @@
+var path = require('path');
+var spmrc = require('spmrc');
+var grunt = require('spm-grunt');
+
+exports = module.exports = function(options) {
+ options = options || {};
+ var pkgfile = options.pkgfile || 'package.json';
+ var pkg = {};
+ if (grunt.file.exists(pkgfile)) {
+ pkg = grunt.file.readJSON('package.json');
+ }
+
+ var installpath = spmrc.get('install.path');
+ options.paths = [installpath];
+ if (installpath !== 'sea-modules') {
+ options.paths.push('sea-modules');
+ }
+ var globalpath = path.join(spmrc.get('user.home'), '.spm', 'sea-modules');
+ options.paths.push(globalpath);
+
+ var scripts = pkg.scripts || {};
+ if (scripts.build) {
+ childexec(scripts.build, function() {
+ log.info('success', 'build finished.');
+ });
+ } else {
+ grunt.invokeTask('build', options, function(grunt) {
+ exports.loadBuildTasks(options, pkg);
+ grunt.task.options({'done': function() {
+ log.info('success', 'build finished.');
+ }});
+ grunt.registerInitTask('build', ['spm-build']);
+ });
+ }
+};
+
+exports.loadBuildTasks = function(options, pkg) {
+ grunt.log.writeln('load spm-build');
+ options = options || {};
+ pkg = pkg || grunt.file.readJSON('package.json');
+
+ var config = {pkg: pkg};
+ config.src = options.inputDirectory || 'src';
+ config.dest = options.outputDirectory || 'dist';
+ config.paths = options.paths || ['sea-modules'];
+
+ require('./lib/config').initConfig(grunt, config);
+
+ [
+ 'grunt-cmd-transport',
+ 'grunt-cmd-concat',
+ 'grunt-contrib-uglify',
+ 'grunt-contrib-copy',
+ 'grunt-contrib-cssmin',
+ 'grunt-contrib-clean'
+ ].forEach(function(task) {
+ var taskdir = path.join(__dirname, 'node_modules', task, 'tasks');
+ if (grunt.file.exists(taskdir)) {
+ grunt.loadTasks(taskdir);
+ }
+ });
+};
View
294 lib/config.js
@@ -0,0 +1,294 @@
+function initConfig(grunt, options, deepMerge) {
+ var path = require('path');
+ process.on('log.warn', function(msg) {
+ grunt.log.warn('warn ' + msg);
+ });
+ process.on('log.info', function(msg) {
+ grunt.log.writeln('info ' + msg);
+ });
+
+ options = options || {};
+
+ var style = require('grunt-cmd-transport').style;
+
+ // grunt-cmd-transport, css to js
+ options.css2jsParser = style.init(grunt).css2jsParser;
+
+ // grunt-cmd-concat, js concat css
+ options.css2js = style.css2js;
+
+ var pkg = options.pkg || 'package.json';
+ if (grunt.util._.isString(pkg)) {
+ pkg = grunt.file.readJSON(pkg);
+ }
+ options.pkg = pkg;
+
+ var data = distConfig(options, pkg);
+
+ // import transport rules
+ data.transport = transportConfig(options, pkg);
+ data.clean = {spm: ['.build'], dist: ['dist']};
+
+ // deepMerge should merge to target
+ if (deepMerge) {
+ grunt.util._.merge(data, grunt.config.data);
+ grunt.config.data = data;
+ } else {
+ grunt.util._.defaults(grunt.config.data, data);
+ }
+
+ grunt.registerTask('newline', function() {
+ grunt.file.recurse('dist', function(f) {
+ var extname = path.extname(f);
+ if (extname === '.js' || extname === '.css') {
+ var text = grunt.file.read(f);
+ if (!/\n$/.test(text)) {
+ grunt.file.write(f, text + '\n');
+ }
+ }
+ });
+ });
+
+ grunt.registerTask(
+ 'spm-build', [
+ 'clean:dist', // delete dist direcotry first
+
+ // build css
+ 'transport:spm', // src/* -> .build/src/*
+ 'concat:css', // .build/src/*.css -> .build/dist/*.css
+
+ // build js (must be invoke after css build)
+ 'transport:css', // .build/dist/*.css -> .build/src/*.css.js
+ 'concat:js', // .build/src/* -> .build/dist/*.js
+
+ // to dist
+ 'copy:spm',
+ 'cssmin:css', // .build/dist/*.css -> dist/*.css
+ 'uglify:js', // .build/dist/*.js -> dist/*.js
+
+ 'clean:spm',
+ 'newline'
+ ]);
+}
+
+exports.initConfig = initConfig;
+
+// transport everything:
+//
+// grunt.initConfig({
+// options: {
+// ...
+// pkg: pkg
+// },
+// files: [{
+// cwd: 'src',
+// src: '**/*',
+// filter: 'isFile',
+// dest: '.build/src
+// }]
+// });
+//
+// transport concated css into js:
+//
+// grunt.initConfig({
+// options: {
+// ...
+// idleading: family/name/version/,
+// parsers: {
+// '.css': [css2jsParser]
+// },
+// files: [{
+// cwd: '.build/dist',
+// src: '**/*.css',
+// filter: 'isFile',
+// dest: '.build/src'
+// }]
+// }
+// })
+function transportConfig(options, pkg) {
+ options = options || {};
+ pkg = pkg || {spm: {}};
+ if (!pkg.spm) {
+ pkg.spm = {};
+ }
+ options.source = options.source || pkg.spm.source || 'src';
+ options.idleading = options.idleading || pkg.spm.idleading;
+ if (pkg.family && pkg.name && pkg.version) {
+ options.idleading = options.idleading || (pkg.family + '/' + pkg.name + '/' + pkg.version + '/');
+ }
+
+ var spmConfig = {
+ files: [{
+ cwd: options.source,
+ src: '**/*',
+ filter: 'isFile',
+ dest: '.build/src'
+ }]
+ };
+
+ // transport concated css into js
+ var cssConfig = {
+ options: {
+ parsers: {
+ '.css': [options.css2jsParser]
+ }
+ },
+ files: [{
+ cwd: '.build/dist',
+ src: '**/*.css',
+ filter: 'isFile',
+ dest: '.build/src'
+ }]
+ };
+ ['paths', 'idleading', 'debug', 'handlebars', 'uglify'].forEach(function(key) {
+ if (options.hasOwnProperty(key)) {
+ spmConfig.options[key] = options[key];
+ cssConfig.options[key] = options[key];
+ }
+ });
+ return {spm: spmConfig, css: cssConfig};
+}
+exports.transportConfig = transportConfig;
+
+
+// concat every css
+//
+// grunt.initConfig({
+// options: {
+// ...
+// paths: []
+// },
+// files: [{
+// cwd: '.build/src',
+// src: '**/*.css',
+// expand: true,
+// dest: '.build/dist'
+// }]
+// })
+//
+
+function distConfig(options, pkg) {
+ if (!pkg.spm) {
+ process.emit('log.warn', 'missing `spm` in package.json');
+ process.emit('log.info', 'read the docs at http://docs.spmjs.org/en/package');
+ pkg.spm = {};
+ }
+
+ var output = pkg.spm.output || {};
+
+ var jsconcats = {};
+ var jsmins = [], cssmins = [];
+ var copies = [];
+
+ if (Array.isArray(output)) {
+ var ret = {};
+ output.forEach(function(name) {
+ ret[name] = [name];
+ });
+ output = ret;
+ }
+
+ Object.keys(output).forEach(function(name) {
+ if (name.indexOf('*') === -1) {
+ if (/\.css$/.test(name)) {
+ cssmins.push({
+ dest: 'dist/' + name,
+ src: output[name].map(function(key) {
+ return '.build/dist/' + key;
+ })
+ });
+
+ // copy debug css
+ name = name.replace(/\.css$/, '-debug.css');
+ copies.push({
+ cwd: '.build/dist',
+ src: name,
+ expand: true,
+ dest: 'dist'
+ });
+
+ } else if (/\.js$/.test(name)) {
+ // concat js
+ jsconcats['.build/dist/' + name] = output[name].map(function(key) {
+ return '.build/src/' + key;
+ });
+
+ jsmins.push({
+ src: ['.build/dist/' + name],
+ dest: 'dist/' + name
+ });
+
+ // create debugfile
+ jsconcats['dist/' + name.replace(/\.js$/, '-debug.js')] = output[name].map(function(key) {
+ return '.build/src/' + key.replace(/\.js$/, '-debug.js');
+ });
+ } else {
+ copies.push({
+ cwd: '.build/src',
+ src: name,
+ expand: true,
+ dest: 'dist'
+ });
+ }
+ } else {
+ copies.push({
+ cwd: '.build/src',
+ src: name,
+ filter: function(src) {
+ if (/-debug\.(js|css)$/.test(src)) {
+ return true;
+ }
+ return !/\.(js|css)$/.test(src);
+ },
+ expand: true,
+ dest: 'dist'
+ });
+ jsmins.push({
+ cwd: '.build/src',
+ src: name,
+ filter: function(src) {
+ if (/-debug.js$/.test(src)) {
+ return false;
+ }
+ return /\.js$/.test(src);
+ },
+ expand: true,
+ dest: 'dist'
+ });
+ cssmins.push({
+ cwd: '.build/src',
+ src: name,
+ filter: function(src) {
+ if (/-debug.css$/.test(src)) {
+ return false;
+ }
+ return /\.css$/.test(src);
+ },
+ expand: true,
+ dest: 'dist'
+ });
+ }
+ });
+ return {
+ concat: {
+ // options should have css2js
+ options: options,
+ js: {files: jsconcats},
+ css: {
+ files: [{
+ cwd: '.build/src/',
+ src: '**/*.css',
+ expand: true,
+ dest: '.build/dist'
+ }]
+ }
+ },
+ cssmin: {
+ options: {keepSpecialComments: 0},
+ css: {files: cssmins}
+ },
+ uglify: {js: {files: jsmins}},
+ copy: {spm: {files: copies}}
+ };
+}
+exports.distConfig = distConfig;
View
16 package.json
@@ -1,6 +1,6 @@
{
"name": "spm-build",
- "description": "The best spm plugin ever.",
+ "description": "build the standard cmd module",
"version": "0.1.0a1",
"homepage": "https://github.com/spmjs/spm-build",
"repository": {
@@ -16,6 +16,18 @@
"url": "https://github.com/spmjs/spm-build/blob/master/LICENSE-MIT"
}
],
+ "dependencies": {
+ "spmrc": "~0.1.3",
+ "grunt-cmd-transport": "~0.2.0",
+ "grunt-cmd-concat": "~0.1.0",
+ "grunt-contrib-uglify": "~0.1.2",
+ "grunt-contrib-copy": "~0.4.0",
+ "grunt-contrib-cssmin": "~0.5.0",
+ "grunt-contrib-clean": "~0.4.0"
+ },
+ "peerDependencies": {
+ "spm-grunt": "~0.1.0"
+ },
"bin": {
"spm-build": "bin/spm-build"
},
@@ -26,4 +38,4 @@
"postinstall": "node scripts/postinstall.js",
"uninstall": "node scripts/uninstall.js"
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.