|
1 |
| -/* eslint-disable camelcase */ |
2 |
| - |
3 |
| -const { readFileSync, readdirSync } = require('fs'); |
4 |
| -const { basename, join } = require('path'); |
5 |
| -const { fileExists, folderExists } = require('./files'); |
6 |
| -require('dotenv').config(); |
7 |
| - |
8 |
| -// eslint-disable-next-line no-process-env |
9 |
| -const { DEVROOT } = process.env; |
10 |
| - |
11 |
| -/** |
12 |
| - * Obtains the package.json file from repo path |
13 |
| - * |
14 |
| - * @param {String} pkgFile file - path/package.json |
15 |
| - * @returns {Object} JSON object |
16 |
| - */ |
17 |
| -const getPackage = (pkgFile) => { |
18 |
| - if(fileExists(pkgFile)) { |
19 |
| - const data = readFileSync(pkgFile, { encoding: 'utf8' }); |
20 |
| - return JSON.parse(data); |
21 |
| - } |
22 |
| - return { error: true }; |
23 |
| -}; |
24 |
| - |
25 |
| -/** |
26 |
| - * Determine if a folder contains a .git folder |
27 |
| - * - does not check that git can process the repo so we can still get false positives |
28 |
| - * @param {String} path |
29 |
| - * @return {Boolean} |
30 |
| - */ |
31 |
| -const isGitRepo = (path) => (basename(path) === '.git') && folderExists(path); |
32 |
| - |
33 |
| -/** |
34 |
| - * Obtains paths of all git repositories |
35 |
| - * - only search down one folder |
36 |
| - * @param {String} folder |
37 |
| - * @param {Array} foldersToInclude |
38 |
| - * @return {Array<string>} path strings |
39 |
| - */ |
40 |
| -const allRepoPaths = (folder = DEVROOT, foldersToInclude = []) => { |
41 |
| - // get files in root |
42 |
| - return readdirSync(folder) |
43 |
| - .filter(name => { |
44 |
| - if(foldersToInclude.length) { |
45 |
| - return foldersToInclude.includes(name); |
46 |
| - } |
47 |
| - return true; |
48 |
| - }) |
49 |
| - .map(name => join(folder, name)) |
50 |
| - .filter(folderExists) |
51 |
| - .filter(path => { |
52 |
| - // returns array of path that end a folder named .git |
53 |
| - const result = readdirSync(path) |
54 |
| - .reduce((accumulator, current) => { |
55 |
| - const subFolder = join(path, current); |
56 |
| - // this is not fool-proof ideally we want to skip bogus repos |
57 |
| - if(isGitRepo(subFolder)) { |
58 |
| - accumulator.push(subFolder); |
59 |
| - } |
60 |
| - return accumulator; |
61 |
| - }, [])[0]; |
62 |
| - return result !== undefined; |
63 |
| - }); |
64 |
| -}; |
65 |
| - |
66 |
| - |
67 |
| -/** |
68 |
| - * Identify the location of the gulp binary |
69 |
| - * |
70 |
| - * @param {String} builderName - from package.builder |
71 |
| - * @param {String} repoPath - path of the repository which release is being run on |
72 |
| - * @param {String} [devRoot] - development root folder |
73 |
| - * @returns {Object} build root and the path to the gulp binary |
74 |
| - * @private |
75 |
| - */ |
76 |
| -const getBinaryPaths = (builderName, repoPath, devRoot = DEVROOT) => { |
77 |
| - const gulpBinary = `node_modules/gulp/bin/gulp.js`; |
78 |
| - try { |
79 |
| - const buildRoot = builderName ? join(devRoot, 'tooling', 'builders', builderName) : repoPath; |
80 |
| - const gulpFile = join(buildRoot, gulpBinary); |
81 |
| - return { buildRoot, gulpFile }; |
82 |
| - } |
83 |
| - // eslint-disable-next-line no-unused-vars |
84 |
| - catch (e) { |
85 |
| - if(e) { |
86 |
| - return {}; |
87 |
| - } |
88 |
| - } |
89 |
| -}; |
90 |
| - |
91 |
| -module.exports = { |
92 |
| - allRepoPaths |
93 |
| - , getBinaryPaths |
94 |
| - , getPackage |
95 |
| -}; |
| 1 | +/* eslint-disable camelcase */ |
| 2 | + |
| 3 | +const { readFileSync, readdirSync } = require('fs'); |
| 4 | +const { basename, join } = require('path'); |
| 5 | +const { fileExists, folderExists } = require('./files'); |
| 6 | +require('dotenv').config(); |
| 7 | + |
| 8 | +// eslint-disable-next-line no-process-env |
| 9 | +const { DEVROOT } = process.env; |
| 10 | + |
| 11 | +/** |
| 12 | + * Obtains the package.json file from repo path |
| 13 | + * |
| 14 | + * @param {String} pkgFile file - path/package.json |
| 15 | + * @returns {Object} JSON object |
| 16 | + */ |
| 17 | +const getPackage = (pkgFile) => { |
| 18 | + if(fileExists(pkgFile)) { |
| 19 | + const data = readFileSync(pkgFile).toString(); |
| 20 | + return JSON.parse(data); |
| 21 | + } |
| 22 | + return { error: true }; |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * Determine if a folder contains a .git folder |
| 27 | + * - does not check that git can process the repo so we can still get false positives |
| 28 | + * @param {String} path |
| 29 | + * @return {Boolean} |
| 30 | + */ |
| 31 | +const isGitRepo = (path) => (basename(path) === '.git') && folderExists(path); |
| 32 | + |
| 33 | +/** |
| 34 | + * Obtains paths of all git repositories |
| 35 | + * - only search down one folder |
| 36 | + * @param {String} folder |
| 37 | + * @param {Array} foldersToInclude |
| 38 | + * @return {Array<string>} path strings |
| 39 | + */ |
| 40 | +const allRepoPaths = (folder = DEVROOT, foldersToInclude = []) => { |
| 41 | + // get files in root |
| 42 | + return readdirSync(folder) |
| 43 | + .filter(name => { |
| 44 | + if(foldersToInclude.length) { |
| 45 | + return foldersToInclude.includes(name); |
| 46 | + } |
| 47 | + return true; |
| 48 | + }) |
| 49 | + .map(name => join(folder, name)) |
| 50 | + .filter(folderExists) |
| 51 | + .filter(path => { |
| 52 | + // returns array of path that end a folder named .git |
| 53 | + const result = readdirSync(path) |
| 54 | + .reduce((accumulator, current) => { |
| 55 | + const subFolder = join(path, current); |
| 56 | + // this is not fool-proof ideally we want to skip bogus repos |
| 57 | + if(isGitRepo(subFolder)) { |
| 58 | + accumulator.push(subFolder); |
| 59 | + } |
| 60 | + return accumulator; |
| 61 | + }, [])[0]; |
| 62 | + return result !== undefined; |
| 63 | + }); |
| 64 | +}; |
| 65 | + |
| 66 | + |
| 67 | +/** |
| 68 | + * Identify the location of the gulp binary |
| 69 | + * |
| 70 | + * @param {String} builderName - from package.builder |
| 71 | + * @param {String} repoPath - path of the repository which release is being run on |
| 72 | + * @param {String} [devRoot] - development root folder |
| 73 | + * @returns {Object} build root and the path to the gulp binary |
| 74 | + * @private |
| 75 | + */ |
| 76 | +const getBinaryPaths = (builderName, repoPath, devRoot = DEVROOT) => { |
| 77 | + const gulpBinary = `node_modules/gulp/bin/gulp.js`; |
| 78 | + try { |
| 79 | + const buildRoot = builderName ? join(devRoot, 'tooling', 'builders', builderName) : repoPath; |
| 80 | + const gulpFile = join(buildRoot, gulpBinary); |
| 81 | + return { buildRoot, gulpFile }; |
| 82 | + } |
| 83 | + // eslint-disable-next-line no-unused-vars |
| 84 | + catch (e) { |
| 85 | + if(e) { |
| 86 | + return {}; |
| 87 | + } |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +module.exports = { |
| 92 | + allRepoPaths |
| 93 | + , getBinaryPaths |
| 94 | + , getPackage |
| 95 | +}; |
0 commit comments