Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nqdeng committed Jan 5, 2013
1 parent 5232ead commit 3f984cb
Show file tree
Hide file tree
Showing 10 changed files with 870 additions and 0 deletions.
63 changes: 63 additions & 0 deletions bin/apm
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#! /usr/bin/env node

/**
* APM - Bin
* Copyright(c) 2012 ~ 2012 Alibaba.com, Inc.
* MIT Licensed
*/

var program = require('commander'),
apm = require('apm'),
version = require('../package.json').version;

var bingo = false; // Is Command parameters accepted.

program
.version(version)
.usage('<command>');

program
.command('build')
.description('Build project')
.action(function (dir) {
bingo = true;
apm.run('build');
});

program
.command('clean')
.description('Clean project directory')
.action(function (dir) {
bingo = true;
apm.run('clean');
});

program
.command('install')
.description('Install dependencies')
.action(function (dir) {
bingo = true;
apm.run('install');
});

program
.command('reset')
.description('Clean module cache')
.action(function (dir) {
bingo = true;
apm.run('reset');
});

program
.command('help')
.description('Display help information')
.action(function () {
bingo = true;
program.help();
});

program.parse(process.argv);

if (!bingo) {
console.log('Type \'apm help\' for usage.');
}
4 changes: 4 additions & 0 deletions bin/spm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /usr/bin/env node

exports.cli = true;
require('spm');
197 changes: 197 additions & 0 deletions lib/action/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* APM - Action - Build
* Copyright(c) 2012 ~ 2013 Alibaba.com, Inc.
* MIT Licensed
*/

var fs = require('fs'),
path = require('path');
spm = require('spm'),
util = require('../util');

var BASE_TMPL = '/js/6v/biz/%s/sea-modules',

CONFIG_TMPL = 'seajs.config({alias:<%=alias%>,base:(location.protocol==="https:"?"https://stylessl.aliunicorn.com":"http://style.aliunicorn.com")+"<%=base%>"});',

SUBMODULE_PATH_TMPL = 'local/%s/%s',

SUBMODULE_PATH_DEV_TMPL = 'local/../../local/%s',

/**
* Generate config files.
*/
config = function () {
var moduleName = pkg().name,
base = util.format(BASE_TMPL, moduleName),
alias = {},
aliasDev = {};

subModules().forEach(function (name) {
var key = moduleName + '.' + name;

alias[key] =
util.format(SUBMODULE_PATH_TMPL, name, version(name));
aliasDev[key] =
util.format(SUBMODULE_PATH_DEV_TMPL, name);
});

util.mix(aliasDev, pkg().dependencies);

fs.writeFileSync('sea-config.js', util.tmpl(CONFIG_TMPL, {
alias: JSON.stringify(alias),
base: base
}));

fs.writeFileSync('sea-config-dev.js', util.tmpl(CONFIG_TMPL, {
alias: JSON.stringify(aliasDev),
base: base
}));
},

/**
* Generate output config for a sub module.
* @param name {string}
* @return {Object}
*/
output = function (name) {
var pathname = path.join('local', name),
customOutput = path.join(pathname, 'output.json'),
output;

if (fs.existsSync(customOutput)) {
return JSON.parse(fs.readFileSync(customOutput, 'utf8'));
} else {
output = {
'i18n/*.js': '.'
};

fs.readdirSync(pathname)
.filter(function (item) {
item = path.join(pathname, item);

return fs.statSync(item).isFile()
&& path.extname(item) === '.js';
})
.forEach(function (item) {
output[item] = '.';
});

return output;
}
},

/**
* Read package.json.
* @return {Object}
*/
pkg = (function () {
var cache = null;

return function () {
if (!cache) {
cache = fs.readFileSync('package.json', 'utf8');
}

return JSON.parse(cache);
};
}()),

/**
* Run action.
* @param next {Function}
*/
run = function (next) {
var build = spm.getAction('build'),
options = task(),
len = options.length,
i = 0;

(function next2() {
if (i < len) {
build.run(options[i++], next2);
} else {
config();
next();
}
}());
},

/**
* Get names of sub modules.
* @return {Array}
*/
subModules = (function () {
var cache = null;

return function () {
if (!cache) {
cache = fs.readdirSync('local')
.filter(function (item) {
return fs.statSync(path.join('local', item))
.isDirectory();
});
}

return cache;
};
}()),

/**
* Generate building option for each sub module.
* @return {Array}
*/
task = function () {
return subModules().map(function (name) {
var modInfo = pkg(),
options = {
baseModInfo: modInfo,
src: path.join('local', name),
to: path.join('sea-modules/local', name, version(name))
};

modInfo.name = name;
modInfo.output = output(name);
modInfo.root = 'local';
modInfo.version = version(name);

return options;
});
},

/**
* Calculate the version number of a sub module.
* @param pathname {string}
* @return {string}
*/
version = (function () {
var cache = {};

return function (name) {
if (!cache[name]) {
var contents = [];

(function travel(pathname) {
fs.readdirSync(pathname)
.forEach(function (item) {
item = path.join(pathname, item);

var stats = fs.statSync(item);

if (stats.isFile() && path.extname(item) === '.js') {
contents.push(fs.readFileSync(item, 'utf8'));
} else if (stats.isDirectory()) {
travel(item);
}
});
}(path.join('local', name)));

cache[name] = util.crc32(contents.join(''));
}

return cache[name];
};
}()),

DUMMY;

module.exports = run;
66 changes: 66 additions & 0 deletions lib/action/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* APM - Action - Clean
* Copyright(c) 2012 ~ 2013 Alibaba.com, Inc.
* MIT Licensed
*/

var svn = require('../svn'),
util = require('../util');

var TARGET = [
'sea-modules',
'sea-config.js',
'sea-config-dev.js'
],

/**
* Remove from simple file system.
* @param target {Array}
*/
simpleRemove = function (target) {
target.forEach(function (pathname) {
util.rm(pathname);
});
},

/**
* Remove from SVN repository.
* @param target {Array}
* @param callback {Function}
*/
svnRemove = function (target, callback) {
(function next(i) {
if (i < target.length) {
svn.rm(target[i], function (err) {
if (err) {
callback(err);
} else {
next(i + 1);
}
});
} else {
callback(null);
}
}(0));
},

/**
* Run action.
* @param next {Function}
*/
run = function (next) {
if (svn.detect(process.cwd())) {
svnRemove(TARGET, function (err) {
if (err) {
throw err;
} else {
next();
}
});
} else {
simpleRemove(TARGET);
next();
}
};

module.exports = run;
22 changes: 22 additions & 0 deletions lib/action/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* APM - Action - Install
* Copyright(c) 2012 ~ 2013 Alibaba.com, Inc.
* MIT Licensed
*/

var spm = require('spm');

var TARGET = 'sea-modules',

/**
* Run action.
* @param next {Function}
*/
run = function (next) {
spm.getAction('install').run({
force: true,
to: TARGET
}, next);
};

module.exports = run;
19 changes: 19 additions & 0 deletions lib/action/reset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* APM - Action - Reset
* Copyright(c) 2012 ~ 2013 Alibaba.com, Inc.
* MIT Licensed
*/

var spm = require('spm');

/**
* Run action.
* @param next {Function}
*/
var run = function (next) {
spm.getAction('env').run({
clean: true,
}, next);
};

module.exports = run;
Loading

0 comments on commit 3f984cb

Please sign in to comment.