motion
Version:
motion - moving development forward
285 lines (227 loc) • 8.37 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.serialize = exports.init = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
let init = exports.init = function () {
var ref = _asyncToGenerator(function* (cli) {
try {
// init
OPTS.appDir = _path2.default.normalize(process.cwd());
OPTS.name = cli.name || _path2.default.basename(process.cwd());
OPTS.saneName = (0, _fns.sanitize)(cli.name);
OPTS.hasRunInitialBuild = false;
OPTS.defaultPort = 4000;
setupCliOpts(cli);
setupDirs();
// from flint
yield migration();
// config
const config = yield loadConfigs(cli);
setupConfig(cli, config);
} catch (e) {
(0, _fns.handleError)(e);
}
});
return function init(_x) {
return ref.apply(this, arguments);
};
}();
// TODO remove in minor
let migration = function () {
var ref = _asyncToGenerator(function* () {
let flintDir = (0, _fns.p)(OPTS.appDir, '.flint');
if (yield (0, _fns.exists)(flintDir)) {
// .flint => .motion
yield (0, _fns.move)(flintDir, OPTS.motionDir);
// index.html "#_flintapp" => "#_motionapp"
(0, _fns.replace)({
regex: '_flintapp',
replacement: '_motionapp',
paths: [OPTS.motionDir],
recursive: true,
silent: true
});
// .motion/flint.json => .motion/config.js
const oldConfLoc = (0, _fns.p)(OPTS.motionDir, 'flint.json');
if (yield (0, _fns.exists)(oldConfLoc)) {
print(` Migrating flint config to motion (flint.json => config.js)...\n`);
yield (0, _fns.move)(oldConfLoc, OPTS.configFile);
// add module.exports
const oldConf = yield (0, _fns.readFile)(OPTS.configFile);
yield (0, _fns.writeFile)(OPTS.configFile, `module.exports = ${ oldConf }`);
}
}
});
return function migration() {
return ref.apply(this, arguments);
};
}();
let loadConfigs = function () {
var ref = _asyncToGenerator(function* () {
let result;
try {
const file = yield parseConfig();
if (file) {
const out = yield (0, _fns.readFile)(file);
result = eval(out);
} else {
print('No .motion/config.js file found!');
}
} catch (e) {
(0, _fns.handleError)(e);
}
return modeMergedConfig(result || {});
});
return function loadConfigs() {
return ref.apply(this, arguments);
};
}();
let setupCliOpts = function () {
var ref = _asyncToGenerator(function* (cli) {
OPTS.version = cli.version;
OPTS.debug = cli.debug;
OPTS.watch = cli.watch;
OPTS.reset = cli.reset;
OPTS.build = cli.build;
OPTS.out = cli.out;
OPTS.watching = cli.watch || !cli.build;
// ensure we dont clobber things
if (cli.out && (yield (0, _fns.exists)(cli.out))) {
console.error(`\n Build dir already exists! Ensure you target an empty directory.\n`.red);
process.exit(1);
}
});
return function setupCliOpts(_x4) {
return ref.apply(this, arguments);
};
}();
let serialize = exports.serialize = function () {
var ref = _asyncToGenerator(function* () {
yield _disk2.default.state.write(function (state, write) {
state.opts = _extends({}, OPTS);
delete state.opts.state; // prevent circular structure
write(state);
});
});
return function serialize() {
return ref.apply(this, arguments);
};
}();
exports.set = set;
exports.get = get;
exports.debug = debug;
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fns = require('./lib/fns');
var _disk = require('./disk');
var _disk2 = _interopRequireDefault(_disk);
var _util = require('util');
var _util2 = _interopRequireDefault(_util);
var _webpack = require('webpack');
var _webpack2 = _interopRequireDefault(_webpack);
var _getWebpackErrors = require('./bundler/lib/getWebpackErrors');
var _getWebpackErrors2 = _interopRequireDefault(_getWebpackErrors);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }
let OPTS = {};
function modeMergedConfig(config) {
const modeSpecificConfig = config[OPTS.build ? 'build' : 'run'];
const merged = Object.assign({}, config, modeSpecificConfig);
return merged;
}
function parseConfig() {
return new Promise((() => {
var ref = _asyncToGenerator(function* (resolve, reject) {
try {
if (!(yield (0, _fns.exists)(OPTS.configFile))) resolve(false);
// for json loader
const runnerRoot = _path2.default.resolve(_path2.default.join(__dirname, '..', '..'));
const runnerModules = _path2.default.join(runnerRoot, 'node_modules');
(0, _webpack2.default)({
context: OPTS.motionDir,
entry: './config.js',
output: {
filename: 'user-config.js',
path: './.motion/.internal',
libraryTarget: 'commonjs2'
},
module: {
loaders: [{ test: /\.json$/, loader: 'json' }]
},
resolveLoader: { root: runnerModules }
}, function (err, stats) {
let error = (0, _getWebpackErrors2.default)('config', err, stats);
if (error) reject(error);else resolve((0, _fns.p)(OPTS.internalDir, 'user-config.js'));
});
} catch (e) {
reject(e);
}
}),
_this = this;
return function (_x2, _x3) {
return ref.apply(_this, arguments);
};
})());
}
function setupDirs() {
// base dirs
OPTS.motionDir = (0, _fns.p)(OPTS.appDir, '.motion');
OPTS.modulesDir = (0, _fns.p)(OPTS.motionDir, 'node_modules');
OPTS.internalDir = (0, _fns.p)(OPTS.motionDir, '.internal');
OPTS.template = OPTS.template || '.motion/index.html';
OPTS.buildDir = OPTS.out ? (0, _fns.p)(OPTS.out) : (0, _fns.p)(OPTS.motionDir, 'build');
// deps dirs
OPTS.deps = {};
OPTS.deps.dir = (0, _fns.p)(OPTS.internalDir, 'deps');
OPTS.deps.internalDir = (0, _fns.p)(OPTS.internalDir, 'deps', 'internal');
OPTS.deps.assetsDir = (0, _fns.p)(OPTS.deps.dir, 'assets');
OPTS.deps.internalsIn = (0, _fns.p)(OPTS.deps.dir, 'internals.in.js');
OPTS.deps.internalsOut = (0, _fns.p)(OPTS.deps.dir, 'internals.js');
OPTS.deps.externalsIn = (0, _fns.p)(OPTS.deps.dir, 'externals.in.js');
OPTS.deps.externalsOut = (0, _fns.p)(OPTS.deps.dir, 'externals.js');
OPTS.deps.externalsPaths = (0, _fns.p)(OPTS.deps.dir, 'externals.paths.js');
OPTS.configFile = (0, _fns.p)(OPTS.motionDir, 'config.js');
OPTS.stateFile = (0, _fns.p)(OPTS.internalDir, 'state.json');
OPTS.outDir = (0, _fns.p)(OPTS.internalDir, 'out');
OPTS.styleDir = (0, _fns.p)(OPTS.internalDir, 'styles');
OPTS.styleOutDir = (0, _fns.p)(OPTS.buildDir, '_');
OPTS.styleOutName = 'styles.css';
}
function setupConfig(cli, config) {
// config
OPTS.config = Object.assign({
minify: true,
debug: false,
routing: true
}, config);
// cli overrides config
if (cli.nomin) OPTS.config.minify = false;
if (cli.pretty) OPTS.config.pretty = true;
if (cli.port) OPTS.config.port = cli.port;
if (cli.host) OPTS.config.host = cli.host;
}
function set(key, val) {
_fns.log.opts('opts.set'.bold.yellow, key, val);
OPTS[key] = val;
return val;
}
function get(key) {
return key ? OPTS[key] : OPTS;
}
function debug() {
print(_util2.default.inspect(OPTS, false, 10));
}
// this is bit funky, but lets us do:
// opts('dir') => path
// opts.set('dir', 'other')
function opts(name) {
return get(name);
}
opts.set = set;
opts.init = init;
opts.serialize = serialize;
opts.debug = debug;
exports.default = opts;
//# sourceMappingURL=opts.js.map
;