Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/codeInstaller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let shell = require('shelljs'),
http = require('http'),
fs = require('fs'),
uglify = require('uglify-js'),
exec = require('child_process').exec,
_ = require('lodash');

function download(url, dest, cb) {
Expand Down Expand Up @@ -41,6 +42,8 @@ function install(code, config) {
.substring(0, 7) + '.js';
let outputFile = path.join(outputDir, hashName);

let [filePath, hash] = resource.split(':');

// handle external resources
if (url.parse(resource).host) {
console.log(`Downloading code ${resource}...`);
Expand All @@ -51,12 +54,24 @@ function install(code, config) {
}
resolve([resource, outputFile]);
});
} else if (fs.existsSync(resource)) {
} else if (fs.existsSync(filePath)) {
console.log(`Copying code file ${resource}...`);

// TODO: maybe add some build tools here, like browserify or webpack and stuff
fs.readFile(resource, 'utf-8', (err, data) => {
if (err) {
if (hash) {
exec('git rev-parse --show-toplevel', (err, gitRoot) => {
if (err) {
return reject(err);
}
gitRoot = gitRoot.trim();
exec(`git show ${hash}:${path.relative(gitRoot, filePath)}`, callback);
});
} else {
fs.readFile(filePath, 'utf-8', callback);
}

function callback(err, data) {
if (err) {
return reject(err);
}

Expand All @@ -74,7 +89,7 @@ function install(code, config) {
}
resolve([resource, outputFile]);
});
});
}
} else {
fs.writeFile(outputFile, resource, (err) => {
if (err) {
Expand Down
12 changes: 7 additions & 5 deletions src/configLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ function resolveFile(cwd, str) {
return str;
}

let [filePath, hash] = str.split(':');

// if it's already an absolute path, don't do anything
if (path.isAbsolute(str)) {
if (path.isAbsolute(filePath)) {
return str;
}

if (str.startsWith('.' + path.sep) || str.startsWith('..' + path.sep)) {
if (filePath.startsWith('.' + path.sep) || filePath.startsWith('..' + path.sep)) {
// otherwise, translate to absolute path from relative
return path.resolve(cwd, str);
return path.resolve(cwd, filePath) + (hash ? ':' + hash : '');
}

return str;
Expand Down Expand Up @@ -110,9 +112,9 @@ function resolveAbsolutePaths(workingDir, configs) {
if (typeof config.packages === 'object') {
_.forEach(config.packages, pkg => {
if (Array.isArray(pkg.code)) {
pkg.code = pkg.code.map(path => fs.existsSync(path) ? resolve(path) : path);
pkg.code = pkg.code.map(path => fs.existsSync(resolve(path).split(':')[0]) ? resolve(path) : path);
} else if (typeof pkg.code === 'object') {
pkg.code = _.mapValues(pkg.code, path => fs.existsSync(path) ? resolve(path) : path);
pkg.code = _.mapValues(pkg.code, path => fs.existsSync(resolve(path).split(':')[0]) ? resolve(path) : path);
}
});
}
Expand Down
8 changes: 5 additions & 3 deletions tests/configLoader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ describe("the configuration loader", () => {
let configs = getConfig('./fixtures/multiplePubVersion.json');

let codeList = getCodeList(configs).sort();
expect(codeList[0]).toMatch('./adUnits.js');
expect(codeList[1]).toMatch('./adUnits2.js');
expect(codeList[2]).toMatch('digitrust.min.js');
expect(codeList[0]).toMatch('fixtures/aTestFile.js:267af7');
expect(codeList[1]).toMatch('fixtures/adUnits.js');
expect(codeList[2]).toMatch('fixtures/adUnits2.js');
expect(codeList[3]).toEqual('console.log(\'hi\');');
expect(codeList[4]).toMatch('digitrust.min.js');
});
});
1 change: 1 addition & 0 deletions tests/fixtures/aTestFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "new";
2 changes: 2 additions & 0 deletions tests/fixtures/multiplePubVersion.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"version": "1.15.0",
"code": {
"adUnits": "./adUnits2.js",
"aTestFile": "./aTestFile.js:267af7",
"javascript": "console.log('hi');",
"digitrust": "http://cdn.digitru.st/prod/1/digitrust.min.js"
}
}
Expand Down