-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* ♻️ extract files.utils.js * ♻️ extract git.utils.js * ♻️ extract secrets.js * ♻️ rename utils.js in execution.utils.js * 👌 rename `*.utils` in `*-utils` * 👌 extract command.js
- Loading branch information
Showing
21 changed files
with
222 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const spawn = require('child_process').spawn | ||
// node-fetch v3.x only support ESM syntax. | ||
// Todo: Remove node-fetch when node v18 LTS is released with fetch out of the box | ||
const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)) | ||
|
||
/** | ||
* Helper to run executables asynchronously, in a shell. This function does not prevent Shell | ||
* injections[0], so please use carefully. Only use it to run commands with trusted arguments. | ||
* Prefer the `command` helper for most use cases. | ||
* | ||
* [0]: https://matklad.github.io/2021/07/30/shell-injection.html | ||
*/ | ||
async function spawnCommand(command, args) { | ||
return new Promise((resolve, reject) => { | ||
const child = spawn(command, args, { stdio: 'inherit', shell: true }) | ||
child.on('error', reject) | ||
child.on('close', resolve) | ||
child.on('exit', resolve) | ||
}) | ||
} | ||
|
||
function runMain(mainFunction) { | ||
Promise.resolve() | ||
// The main function can be either synchronous or asynchronous, so let's wrap it in an async | ||
// callback that will catch both thrown errors and rejected promises | ||
.then(() => mainFunction()) | ||
.catch((error) => { | ||
printError('\nScript exited with error:') | ||
printError(error) | ||
process.exit(1) | ||
}) | ||
} | ||
|
||
const resetColor = '\x1b[0m' | ||
|
||
function printError(...params) { | ||
const redColor = '\x1b[31;1m' | ||
console.log(redColor, ...params, resetColor) | ||
} | ||
|
||
function printLog(...params) { | ||
const greenColor = '\x1b[32;1m' | ||
console.log(greenColor, ...params, resetColor) | ||
} | ||
|
||
async function fetchWrapper(url, options) { | ||
const response = await fetch(url, options) | ||
if (!response.ok) { | ||
throw new Error(`HTTP Error Response: ${response.status} ${response.statusText}`) | ||
} | ||
|
||
return response.text() | ||
} | ||
|
||
module.exports = { | ||
spawnCommand, | ||
printError, | ||
printLog, | ||
runMain, | ||
fetch: fetchWrapper, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const fsPromises = require('fs/promises') | ||
|
||
const { command } = require('./command') | ||
|
||
const CI_FILE = '.gitlab-ci.yml' | ||
|
||
function readCiFileVariable(variableName) { | ||
const regexp = new RegExp(`${variableName}: (.*)`) | ||
const ciFileContent = fs.readFileSync(CI_FILE, { encoding: 'utf-8' }) | ||
return regexp.exec(ciFileContent)?.[1] | ||
} | ||
|
||
async function replaceCiFileVariable(variableName, value) { | ||
await modifyFile(CI_FILE, (content) => | ||
content.replace(new RegExp(`${variableName}: .*`), `${variableName}: ${value}`) | ||
) | ||
} | ||
|
||
/** | ||
* @param filePath {string} | ||
* @param modifier {(content: string) => string} | ||
*/ | ||
async function modifyFile(filePath, modifier) { | ||
const content = await fsPromises.readFile(filePath, { encoding: 'utf-8' }) | ||
const modifiedContent = modifier(content) | ||
if (content !== modifiedContent) { | ||
await fsPromises.writeFile(filePath, modifiedContent) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
function findBrowserSdkPackageJsonFiles() { | ||
const manifestPaths = command`git ls-files -- package.json */package.json`.run() | ||
return manifestPaths | ||
.trim() | ||
.split('\n') | ||
.map((manifestPath) => { | ||
const absoluteManifestPath = path.join(__dirname, '../..', manifestPath) | ||
return { | ||
relativePath: manifestPath, | ||
path: absoluteManifestPath, | ||
content: require(absoluteManifestPath), | ||
} | ||
}) | ||
} | ||
|
||
module.exports = { | ||
CI_FILE, | ||
readCiFileVariable, | ||
replaceCiFileVariable, | ||
modifyFile, | ||
findBrowserSdkPackageJsonFiles, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const os = require('os') | ||
const fs = require('fs') | ||
|
||
const { command } = require('../lib/command') | ||
const { getGithubDeployKey } = require('./secrets') | ||
|
||
function initGitConfig(repository) { | ||
const homedir = os.homedir() | ||
|
||
// ssh-add expects a new line at the end of the PEM-formatted private key | ||
// https://stackoverflow.com/a/59595773 | ||
command`ssh-add -`.withInput(`${getGithubDeployKey()}\n`).run() | ||
command`mkdir -p ${homedir}/.ssh`.run() | ||
command`chmod 700 ${homedir}/.ssh`.run() | ||
const sshHost = command`ssh-keyscan -H github.com`.run() | ||
fs.appendFileSync(`${homedir}/.ssh/known_hosts`, sshHost) | ||
command`git config user.email ci.browser-sdk@datadoghq.com`.run() | ||
command`git config user.name ci.browser-sdk`.run() | ||
command`git remote set-url origin ${repository}`.run() | ||
} | ||
|
||
module.exports = { | ||
initGitConfig, | ||
} |
Oops, something went wrong.