UNPKG

tilda-init

Version:
75 lines (67 loc) 2.63 kB
"use strict"; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var isThere = require("is-there"), ul = require("ul"), path = require("path"), abs = require("abs"), rJson = require("r-package-json"), wJson = require("w-package-json"), oneByOne = require("one-by-one"), writeFile = require("write-file-p"), camelo = require("camelo"), npm = require("spawn-npm"), fs = require("fs"); /** * tildaInit * Init the cli interface. * * @name tildaInit * @function * @param {String} dir The dir path. * @param {Object} opts An object containing: * * - `path` (String): The test script name (default: `bin/{name}`) * * @param {Function} cb The callback function. */ module.exports = function testerInit(dir, opts, cb) { if (typeof opts === "function") { cb = opts; opts = {}; } var indexPath = ""; oneByOne([function (next) { return rJson(dir, next); }, function (next, pack) { opts = ul.merge({ path: "bin/" + pack.name + ".js" }); dir = path.normalize(abs(dir)); indexPath = path.join(dir, opts.path); if (pack.bin && typeof pack.bin === "string") { pack.bin = _defineProperty({}, pack.name, pack.bin); } pack = ul.merge(pack, { bin: {} }); pack.bin = pack.bin || {}; pack.bin[pack.name] = opts.path; wJson(dir, pack, function (err) { return next(err, pack); }); }, function (next, pack) { return isThere(indexPath, function (exists) { return next(exists && new Error("The executable file already exists. Refusing to override it."), pack); }); }, function (next, pack) { var cName = camelo(pack.name); writeFile(indexPath, "#!/usr/bin/env node\n\"use strict\";\n\nconst Tilda = require(\"tilda\")\n , " + cName + " = require(\"..\")\n ;\n\nnew Tilda(`${__dirname}/../package.json`, {\n options: [\n {\n name: \"foo\"\n , opts: [\"f\", \"foo\"]\n , desc: \"Dummy argument\"\n }\n ]\n}).main(action => {\n /* Do something there */\n console.log(\"Hello World!\");\n});", next); }, function (next) { npm("install", { "save": true, _: ["tilda"] }, { cwd: dir, _showOutput: true }, next); }, function (next) { return fs.chmod(indexPath, "755", next); }], cb); };