From 267af713842ac6a5813327228fa74e212245a3c5 Mon Sep 17 00:00:00 2001 From: Rich Snapp Date: Tue, 4 Sep 2018 14:45:39 -0600 Subject: [PATCH 1/3] start of versioned file reading --- tests/fixtures/aTestFile.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/fixtures/aTestFile.js diff --git a/tests/fixtures/aTestFile.js b/tests/fixtures/aTestFile.js new file mode 100644 index 0000000..c2f3891 --- /dev/null +++ b/tests/fixtures/aTestFile.js @@ -0,0 +1 @@ +module.exports = "old"; \ No newline at end of file From 6209f64e4ee862cd8e194652591f7a3c053cffac Mon Sep 17 00:00:00 2001 From: Rich Snapp Date: Tue, 4 Sep 2018 14:45:57 -0600 Subject: [PATCH 2/3] start of versioned file reading with new versioned file --- tests/fixtures/aTestFile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/aTestFile.js b/tests/fixtures/aTestFile.js index c2f3891..b3d4f8e 100644 --- a/tests/fixtures/aTestFile.js +++ b/tests/fixtures/aTestFile.js @@ -1 +1 @@ -module.exports = "old"; \ No newline at end of file +module.exports = "new"; \ No newline at end of file From b13fda42c2422ccbd9c97e9326b45b467582ac7b Mon Sep 17 00:00:00 2001 From: Rich Snapp Date: Thu, 6 Sep 2018 12:38:20 -0600 Subject: [PATCH 3/3] added ability to read files from git hashes --- src/codeInstaller.js | 23 +++++++++++++++++++---- src/configLoader.js | 12 +++++++----- tests/configLoader.test.js | 8 +++++--- tests/fixtures/multiplePubVersion.json | 2 ++ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/codeInstaller.js b/src/codeInstaller.js index 06b2f28..425e485 100644 --- a/src/codeInstaller.js +++ b/src/codeInstaller.js @@ -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) { @@ -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}...`); @@ -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); } @@ -74,7 +89,7 @@ function install(code, config) { } resolve([resource, outputFile]); }); - }); + } } else { fs.writeFile(outputFile, resource, (err) => { if (err) { diff --git a/src/configLoader.js b/src/configLoader.js index 037b0d1..4399140 100644 --- a/src/configLoader.js +++ b/src/configLoader.js @@ -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; @@ -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); } }); } diff --git a/tests/configLoader.test.js b/tests/configLoader.test.js index 7c457cd..4fe5e7e 100644 --- a/tests/configLoader.test.js +++ b/tests/configLoader.test.js @@ -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'); }); }); \ No newline at end of file diff --git a/tests/fixtures/multiplePubVersion.json b/tests/fixtures/multiplePubVersion.json index aa6cfa9..13c929c 100644 --- a/tests/fixtures/multiplePubVersion.json +++ b/tests/fixtures/multiplePubVersion.json @@ -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" } }