-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·130 lines (107 loc) · 3.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var path = require('path');
var spmrc = require('spmrc');
var grunt = require('spm-grunt');
var getConfig = require('./lib/config').getConfig;
var _cache;
exports = module.exports = function(options) {
process.on('log.warn', function(msg) {
grunt.log.warn('warn ' + msg);
});
process.on('log.info', function(msg) {
grunt.log.writeln('info ' + msg);
});
_cache = options;
options = parseOptions(options);
var pkg = options.pkg;
// console.log(options);
//options.dest='dist/1.0.0'
// check pkg
if (!pkg.spm) {
throw new Error('spm is required in package.json');
}
if (!pkg.spm.output) {
throw new Error('spm.output is required in package.json');
}
var scripts = pkg.scripts || {};
if (scripts.build) {
if (scripts.build.trim() === 'spm build') {
throw new Error('spm build error');
}
childexec(scripts.build, function() {
grunt.log.writeln('success build finished.');
});
} else {
grunt.invokeTask('build', options, function(grunt) {
var config = getConfig(options);
grunt.initConfig(config);
loadTasks();
grunt.task.options({'done': function() {
grunt.log.writeln('success build finished.');
}});
grunt.registerInitTask(
'build', [
'clean:build', // delete build direcotry first
'spm-install', // install dependencies
// build css
'transport:src', // src/* -> .build/src/*
'concat:css', // .build/src/*.css -> .build/tmp/*.css
// build js (must be invoke after css build)
'transport:css', // .build/tmp/*.css -> .build/src/*.css.js
'concat:js', // .build/src/* -> .build/dist/*.js
// to ./build/dist
'copy:build',
'cssmin:css', // .build/tmp/*.css -> .build/dist/*.css
'uglify:js', // .build/tmp/*.js -> .build/dist/*.js
'clean:dist',
'copy:dist', // .build/dist -> dist
'storeinc:js', // dist -> dist
'clean:build',
'spm-newline'
]);
});
}
};
Object.defineProperty(exports, 'config', {
get: function() {
var options = parseOptions(_cache);
return getConfig(options);
}
});
exports.getConfig = getConfig;
exports.parseOptions = parseOptions;
function parseOptions(options) {
options = options || _cache || {};
var pkgfile = options.pkgfile || 'package.json';
var pkg = {};
if (grunt.file.exists(pkgfile)) {
pkg = grunt.file.readJSON('package.json');
}
options.pkg = pkg;
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);
return options;
}
function loadTasks() {
// load built-in tasks
[
'grunt-cmd-transport',
'grunt-cmd-concat',
'grunt-contrib-uglify',
'grunt-contrib-copy',
'grunt-contrib-cssmin',
'grunt-contrib-clean',
'grunt-spm-storeinc'
].forEach(function(task) {
var taskdir = path.join(__dirname, 'node_modules', task, 'tasks');
if (grunt.file.exists(taskdir)) {
grunt.loadTasks(taskdir);
}
});
grunt.loadTasks(path.join(__dirname, 'tasks'));
}
exports.loadTasks = loadTasks;