Skip to content

Commit b01a8db

Browse files
committed
🎨 added instrumentation for package diff and pushing package.json
1 parent f14755b commit b01a8db

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

bin/commands/runs.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const archiver = require("../helpers/archiver"),
1919
downloadBuildArtifacts = require('../helpers/buildArtifacts').downloadBuildArtifacts,
2020
downloadBuildStacktrace = require('../helpers/downloadBuildStacktrace').downloadBuildStacktrace,
2121
updateNotifier = require('update-notifier'),
22-
pkg = require('../../package.json');
22+
pkg = require('../../package.json'),
23+
packageDiff = require('../helpers/package-diff');
2324
const { getStackTraceUrl } = require('../helpers/sync/syncSpecsLogs');
2425

2526
module.exports = function run(args, rawArgs) {
@@ -164,7 +165,14 @@ module.exports = function run(args, rawArgs) {
164165

165166
let test_zip_size = utils.fetchZipSize(path.join(process.cwd(), config.fileName));
166167
let npm_zip_size = utils.fetchZipSize(path.join(process.cwd(), config.packageFileName));
167-
168+
169+
//Package diff
170+
let isPackageDiff = false;
171+
if(!md5data.zipUrlPresent){
172+
isPackageDiff = packageDiff.run(`package.json`, `${config.packageDirName}/package.json`);
173+
logger.debug(`Package difference was ${isPackageDiff ? `found` : `not found`}`);
174+
}
175+
168176
// Uploaded zip file
169177
logger.debug("Started uploading the test suite zip");
170178
logger.debug("Started uploading the node_module zip");
@@ -270,6 +278,9 @@ module.exports = function run(args, rawArgs) {
270278
test_suite_zip_upload: md5data.zipUrlPresent ? 0 : 1,
271279
package_zip_upload: md5data.packageUrlPresent ? 0 : 1
272280
};
281+
if(dataToSend.test_suite_zip_upload === 1 ){
282+
dataToSend['is_package_diff'] = isPackageDiff;
283+
}
273284

274285
if (!md5data.zipUrlPresent && zip.tests_upload_time) {
275286
dataToSend.test_suite_zip_size = parseFloat((test_zip_size / 1024).toFixed(2));

bin/helpers/archiver.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const config = require('./config.js');
23
const fs = require("fs"),
34
path = require("path");
45

@@ -76,6 +77,14 @@ const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
7677
archive.append(packageJSONString, {name: `${cypressAppendFilesZipLocation}browserstack-package.json`});
7778
}
7879

80+
//Create copy of package.json
81+
if(fs.existsSync('package.json')){
82+
let originalPackageJson = JSON.parse(fs.readFileSync('package.json'));
83+
let originalPackageJsonString = JSON.stringify(originalPackageJson, null, 4);
84+
archive.append(originalPackageJsonString, {name: `${cypressAppendFilesZipLocation}userPackage.json`});
85+
logger.debug(`Created copy of package.json in ${config.packageDirName} folder`)
86+
}
87+
7988
// do not add cypress.json if arg provided is false
8089
if (
8190
runSettings.cypress_config_file &&

bin/helpers/package-diff.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict'
2+
const fs = require('fs')
3+
const path = require('path')
4+
const logger = require("./logger").winstonLogger;
5+
6+
exports.run = (basePath, comparePath) => {
7+
if (!basePath || !comparePath) {
8+
logger.debug('Skipping package difference check.')
9+
}
10+
11+
const base = readModules(basePath)
12+
const compare = readModules(comparePath)
13+
14+
let isDiff = false;
15+
Object.keys(base.deps).forEach(baseKey => {
16+
if (baseKey in compare.deps) {
17+
if (base.deps[baseKey] === compare.deps[baseKey]) {
18+
//ignore this check
19+
} else {
20+
isDiff = true;
21+
return;
22+
}
23+
} else {
24+
isDiff = true;
25+
return;
26+
}
27+
})
28+
return isDiff;
29+
}
30+
const readModules = (location) => {
31+
const table = {}
32+
33+
// Resolve package dependencies
34+
if (location.indexOf('package.json') !== -1) {
35+
const data = fs.readFileSync(location.replace(':dev', ''), 'utf-8')
36+
let parsed
37+
try { parsed = JSON.parse(data) } catch (e) { parsed = false }
38+
if (!parsed) { return }
39+
40+
const depsKey = location.indexOf(':dev') !== -1 ? 'devDependencies' : 'dependencies'
41+
const deps = parsed[depsKey] ? parsed[depsKey] : (parsed.dependencies || parsed.devDependencies)
42+
43+
Object.keys(deps).forEach(key => {
44+
deps[key] = deps[key].replace(/\^|~/g, '')
45+
})
46+
return {
47+
name: `${location} {${depsKey}}`,
48+
deps,
49+
}
50+
}
51+
52+
fs.readdirSync(location)
53+
.filter(name => name !== '.bin')
54+
.map(name => {
55+
const pkg = path.join(location, name, 'package.json')
56+
const exists = fs.existsSync(pkg)
57+
if (!exists) { return }
58+
59+
const data = fs.readFileSync(pkg, 'utf-8')
60+
let parsed
61+
62+
try { parsed = JSON.parse(data) } catch (e) { parsed = false }
63+
if (!parsed) { return }
64+
65+
table[name] = parsed.version
66+
})
67+
return {
68+
name: location,
69+
deps: table,
70+
}
71+
}

0 commit comments

Comments
 (0)