Skip to content
This repository has been archived by the owner on Feb 18, 2019. It is now read-only.

Commit

Permalink
feat(dtsm): remove reference from bundle when uninstall definitions
Browse files Browse the repository at this point in the history
Merge branch 'ukyo-uninstall-and-remove-reference-path'
  • Loading branch information
vvakame committed Apr 30, 2016
2 parents a0fe795 + 7733e8e commit 4d0a151
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
33 changes: 28 additions & 5 deletions lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ export default class Manager {
let unused = result.dependenciesList.every(exDep => exDep.depName !== dep.depName);
if (unused) {
this._removeDefinitionFile(this.savedRecipe, dep);

if (!this.savedRecipe.bundle) {
return;
}
let depPath = _path.join(_path.dirname(this.configPath), this.path, dep.depName);
this._removeReferenceFromBundle(this.savedRecipe, depPath);
}
});

Expand Down Expand Up @@ -589,6 +595,14 @@ export default class Manager {
}
}

_createReferenceComment(bundlePath: string, pathFromCwd: string) {
let referencePath = _path.relative(_path.dirname(bundlePath), pathFromCwd);
if (_path.posix) { // for windows
referencePath = referencePath.replace(new RegExp("\\" + _path.win32.sep, "g"), _path.posix.sep);
}
return `/// <reference path="${referencePath}" />` + "\n";
}

_addReferenceToBundle(recipe: m.Recipe, pathFromCwd: string) {
let bundleContent = "";
let bundlePath = _path.join(_path.dirname(this.configPath), recipe.bundle);
Expand All @@ -597,16 +611,25 @@ export default class Manager {
} else {
mkdirp.sync(_path.dirname(bundlePath));
}
let referencePath = _path.relative(_path.dirname(bundlePath), pathFromCwd);
if (_path.posix) { // for windows
referencePath = referencePath.replace(new RegExp("\\" + _path.win32.sep, "g"), _path.posix.sep);
}
let referenceComment = `/// <reference path="${referencePath}" />` + "\n";
let referenceComment = this._createReferenceComment(bundlePath, pathFromCwd);
if (bundleContent.indexOf(referenceComment) === -1) {
fs.appendFileSync(bundlePath, referenceComment, { encoding: "utf8" });
}
}

_removeReferenceFromBundle(recipe: m.Recipe, pathFromCwd: string) {
let bundleContent = "";
let bundlePath = _path.join(_path.dirname(this.configPath), recipe.bundle);
if (!fs.existsSync(bundlePath)) {
return;
}
bundleContent = fs.readFileSync(bundlePath, "utf8");
let referenceComment = this._createReferenceComment(bundlePath, pathFromCwd);
if (bundleContent.indexOf(referenceComment) !== -1) {
fs.writeFileSync(bundlePath, bundleContent.replace(referenceComment, ''), { encoding: "utf8" });
}
}

refs(): Promise<fsgit.RefInfo[]> {
let promises: Promise<any>[];
if (this.offline) {
Expand Down
5 changes: 5 additions & 0 deletions test/cliSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ describe("command line interface", () => {

it("can uninstall definition files", () => {
var targetFile = path.resolve(testWorkingDir, "dtsm.json");
var bundleFile = path.resolve(testWorkingDir, "typings/bundle.d.ts");

assert(!fs.existsSync(targetFile));
return Promise.resolve(null)
Expand Down Expand Up @@ -322,6 +323,8 @@ describe("command line interface", () => {
assert(exit === 0);
assert(fs.existsSync(targetFile));
assert(fs.existsSync(path.resolve(testWorkingDir, "typings/es6-promise/es6-promise.d.ts")));
assert(fs.existsSync(bundleFile));
assert(fs.readFileSync(bundleFile).indexOf('/// <reference path="es6-promise/es6-promise.d.ts" />') !== -1);
resolve();
});
});
Expand All @@ -339,6 +342,8 @@ describe("command line interface", () => {
assert(exit === 0);
assert(fs.existsSync(targetFile));
assert(!fs.existsSync(path.resolve(testWorkingDir + "typings/es6-promise/es6-promise.d.ts")));
assert(fs.existsSync(bundleFile));
assert(fs.readFileSync(bundleFile).indexOf('/// <reference path="es6-promise/es6-promise.d.ts" />') === -1);
resolve();
});
});
Expand Down

0 comments on commit 4d0a151

Please sign in to comment.