Skip to content

Commit

Permalink
simplify file system manager
Browse files Browse the repository at this point in the history
  • Loading branch information
bttmly committed Jul 2, 2016
1 parent d4e1a3e commit a3b1f6c
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 16 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
59 changes: 59 additions & 0 deletions run.js
Original file line number Diff line number Diff line change
@@ -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;
38 changes: 28 additions & 10 deletions tsc/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
///<reference path="../typings/globals/node/index.d.ts"/>

const path = require("path");
const glob = require("glob");
const fs = require("fs-extra");
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions tsc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tsc/make-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"exclude": [
"node_modules"
],
"outDir": "./lib"
"outDir": "./built"
}
1 change: 0 additions & 1 deletion typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit a3b1f6c

Please sign in to comment.