Skip to content

Commit

Permalink
Merge pull request #11 from cuaklabs/chore/add-scripts-package
Browse files Browse the repository at this point in the history
Add scripts package
  • Loading branch information
notaphplover authored Sep 6, 2023
2 parents 7c75d1d + 1558acf commit b93685a
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@cuaklabs/eslint-config": "workspace:*",
"@cuaklabs/jest-config": "workspace:*",
"@cuaklabs/prettier-config": "workspace:*",
"@cuaklabs/scripts": "workspace:*",
"@cuaklabs/typescript-config": "workspace:*",
"husky": "^8.0.3",
"lint-staged": "^14.0.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/tools/scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# npm installed packages
/node_modules/
5 changes: 5 additions & 0 deletions packages/tools/scripts/.lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"*.js": [
"prettier --write"
]
}
24 changes: 24 additions & 0 deletions packages/tools/scripts/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
38 changes: 38 additions & 0 deletions packages/tools/scripts/bin/getAffectedProjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env node

import { argv } from 'node:process';
import { promisifiedExec } from '../src/promisifiedExec.js';

const TURBOREPO_ROOT_PACKAGE_NAME = '//';
const TURBOREPO_TASK_NOT_FOUND_MAGIC_STRING = '\u003cNONEXISTENT\u003e';

const taskToDryRun = argv[2];
const baseRef = argv[3];

let execCommand = `pnpm exec turbo run ${taskToDryRun} --dry=json`;

if (baseRef !== undefined) {
execCommand += ` --filter ...[${baseRef}]`;
}

const stringifiedDryRun = (await promisifiedExec(execCommand)).trim();

const dryRunResult = JSON.parse(stringifiedDryRun);

/** @type {Array.<string>} */
const dryResultPackageNames = dryRunResult.packages.filter(
(packageName) => packageName !== TURBOREPO_ROOT_PACKAGE_NAME,
);

const tasks = dryRunResult.tasks.filter((task) =>
dryResultPackageNames.some(
(packageName) =>
task.taskId === `${packageName}#${taskToDryRun}` &&
!task.command.includes(TURBOREPO_TASK_NOT_FOUND_MAGIC_STRING),
),
);

/** @type {Array.<string>} */
const packageNames = tasks.map((task) => task.package);

console.log(JSON.stringify(packageNames));
19 changes: 19 additions & 0 deletions packages/tools/scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@cuaklabs/scripts",
"private": true,
"description": "common scripts",
"author": "Roberto Pintos López (https://github.com/notaphplover)",
"license": "See license in \"LICENSE\" file",
"type": "module",
"homepage": "https://github.com/cuaklabs/ajstt#readme",
"bugs": {
"url": "https://github.com/cuaklabs/ajstt/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/cuaklabs/ajstt.git"
},
"bin": {
"script-get-affected-projects": "./bin/getAffectedProjects.js"
}
}
3 changes: 3 additions & 0 deletions packages/tools/scripts/prettier.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import config from '@cuaklabs/prettier-config';

export default config;
42 changes: 42 additions & 0 deletions packages/tools/scripts/src/promisifiedExec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { exec } from 'node:child_process';

/**
* @param {string} command command
* @param {PromisifiedExecOptions} options true to pipe standard input output and error streams
* @returns {Promise<string>}
*/
export async function promisifiedExec(command, options) {
options = {
cwd: options?.cwd,
interactive: options?.interactive ?? false,
};

return new Promise((resolve, reject) => {
const childProcess = exec(
command,
{
cwd: options.cwd,
},
(error, stdout) => {
if (error === null) {
resolve(stdout);
} else {
reject(error);
}
},
);

if (options.interactive) {
childProcess.stdout.pipe(process.stdout);
childProcess.stderr.pipe(process.stderr);

process.stdin.pipe(childProcess.stdin);
}
});
}

/**
* @typedef {Object} PromisifiedExecOptions
* @property {boolean} interactive
* @property {string} cwd
*/

0 comments on commit b93685a

Please sign in to comment.