From a3b1f6cae8a4bafa5028f5a91b911e9892bd8cb5 Mon Sep 17 00:00:00 2001 From: Nick Bottomley Date: Fri, 1 Jul 2016 18:35:34 -0700 Subject: [PATCH] simplify file system manager --- Makefile | 8 ++++--- package.json | 2 ++ run.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++ tsc/file-system.ts | 38 +++++++++++++++++++++-------- tsc/index.ts | 2 ++ tsc/make-config.ts | 2 +- tsconfig.json | 2 +- typings.json | 1 - 8 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 run.js diff --git a/Makefile b/Makefile index 2a74ddc..55b904e 100644 --- a/Makefile +++ b/Makefile @@ -8,12 +8,14 @@ events: ./bin/perturb -r ./examples/event-emitter example: - ./node_modules/.bin/babel-node ./src/run.js example + ./node_modules/.bin/tsc + node ./run.js example dogfood: - ./node_modules/.bin/babel-node ./src/run.js dogfood + ./node_modules/.bin/tsc + node ./run.js dogfood build: - ./node_modules/babel-cli/bin/babel.js --out-dir built src + ./node_modules/.bin/tsc .PHONY: test example diff --git a/package.json b/package.json index 30baddf..0a797a1 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "estraverse": "^4.2.0", "fs-extra": "^0.16.5", "glob": "^4.5.3", + "graceful-fs": "^4.1.4", "highland": "^2.5.1", "immutable": "^3.5.0", "intercept-require": "^0.6.1", @@ -57,6 +58,7 @@ "jshint": "^2.6.0", "lodash": "^3.9.2", "sync-exec": "^0.4.0", + "typescript": "^1.8.10", "watch": "^0.13.0" }, "scripts": { diff --git a/run.js b/run.js new file mode 100644 index 0000000..fcaa6e6 --- /dev/null +++ b/run.js @@ -0,0 +1,59 @@ +const path = require("path"); + +function run (perturb, which, cb) { + let config; + + switch (which) { + case "dogfood": + config = { + rootDir: path.join(__dirname, ".."), + runner: "mocha", + }; + break; + + case "example": + config = { + rootDir: path.join(__dirname, "../examples/toy-lib"), + runner: "mocha", + }; + break; + + default: + throw new Error("Unknown config " + which); + } + + if (cb) { + return perturb(config, function (err, results) { + if (err) { + console.log("fatal error in perturb"); + console.log(err); + console.log(err.stack); + cb(err); + process.exit(1); + } + + try { + console.log("kill count", results.filter(r => r.error).length, "/", results.length) + } catch (e) {} + cb(null, results); + }); + } + + return perturb(config) + .then(function (results) { + console.log("DONE!"); + console.log("kill count", results.filter(r => r.error).length, "/", results.length) + return results; + }).catch(function (err) { + console.log("fatal error in perturb"); + console.log(err); + console.log(err.stack); + process.exit(1); + }); +} + +if (!module.parent) { + run(require("./built"), process.argv[2]); +} + +module.exports = run; \ No newline at end of file diff --git a/tsc/file-system.ts b/tsc/file-system.ts index ff4b983..580e86e 100644 --- a/tsc/file-system.ts +++ b/tsc/file-system.ts @@ -1,3 +1,5 @@ +/// + const path = require("path"); const glob = require("glob"); const fs = require("fs-extra"); @@ -9,18 +11,34 @@ const shouldSymlink = new Set([ "node_modules" ]); -function setupPerturbDirectory (config): void { +function setupPerturbDirectory (config: PerturbConfig): void { + + const {projectRoot, sourceDir, testDir, perturbDir} = config; + + const sourceAbs = path.join(projectRoot, sourceDir); + const testAbs = path.join(projectRoot, testDir); + + const pAbs = path.join(projectRoot, perturbDir); + const pSourceAbs = path.join(pAbs, sourceDir); + const pTestAbs = path.join(pAbs, testDir); + + console.log({ projectRoot, sourceAbs, testAbs, pAbs, pSourceAbs, pTestAbs }) // maybe remove this? if it exists it means there is a bug with cleanup - fs.removeSync(config.perturbRoot); - fs.mkdirSync(config.perturbRoot); - fs.copySync(config.originalSourceDir, config.perturbSourceDir); - fs.copySync(config.originalTestDir, config.perturbTestDir); - - fs.readdirSync(config.rootDir) - .filter(f => shouldSymlink.has(f)) - .map(item => [path.join(config.rootDir, item), path.join(config.perturbRoot, item)]) - .forEach(R.apply(fs.symlinkSync)) + try { + fs.removeSync(pAbs); + } catch (e) { + console.log("had to remove .perturb working directory... last run did not cleanup"); + } + + fs.mkdirSync(pAbs); + fs.copySync(sourceAbs, pSourceAbs); + fs.copySync(testAbs, pTestAbs); + + // fs.readdirSync(config.rootDir) + // .filter(f => shouldSymlink.has(f)) + // .map(item => [path.join(config.rootDir, item), path.join(config.perturbRoot, item)]) + // .forEach(R.apply(fs.symlinkSync)) } function teardownPerturbDirectory (config): void { diff --git a/tsc/index.ts b/tsc/index.ts index 4159599..de3a0f5 100644 --- a/tsc/index.ts +++ b/tsc/index.ts @@ -27,6 +27,8 @@ function hasTests (m: Match): boolean { } module.exports = function perturb (_cfg: PerturbConfig) { + console.log("START..."); + const cfg = makeConfig(_cfg); const {setup, teardown, paths} = fileSystem(cfg); diff --git a/tsc/make-config.ts b/tsc/make-config.ts index d72c268..ca2c8e6 100644 --- a/tsc/make-config.ts +++ b/tsc/make-config.ts @@ -28,7 +28,7 @@ module.exports = function makeConfig (userConfig = {}): PerturbConfig { let str = fs.readFileSync(`${process.cwd()}/${CONFIG_FILE_NAME}`).toString(); fileConfig = JSON.parse(str); } catch (err) { - console.log("error finding configuration file", err); + console.log("No config file present"); fileConfig = {}; } diff --git a/tsconfig.json b/tsconfig.json index f30e1ce..c9560f0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,5 +10,5 @@ "exclude": [ "node_modules" ], - "outDir": "./lib" + "outDir": "./built" } \ No newline at end of file diff --git a/typings.json b/typings.json index 12a0058..6614c04 100644 --- a/typings.json +++ b/typings.json @@ -11,7 +11,6 @@ "estree": "registry:dt/estree#0.0.0+20160317120654", "fs-extra": "registry:dt/fs-extra#0.0.0+20160517121359", "immutable": "registry:dt/immutable#3.8.1+20160608070634", - "mocha": "registry:dt/mocha#2.2.5+20160619032855", "node": "registry:dt/node#6.0.0+20160613154055" } }