Skip to content

Instantly share code, notes, and snippets.

@renoirb
Last active September 14, 2021 18:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renoirb/b23b2d60fbe1ca25950005bb4056458d to your computer and use it in GitHub Desktop.
Save renoirb/b23b2d60fbe1ca25950005bb4056458d to your computer and use it in GitHub Desktop.
rush post-rush-install script example
"use strict";
/**
* In order to run on Linux/Unix and Windows, we can't use `#!/usr/bin/env node` at the top.
*
* After `rush update`, copy files to the monorepo root.
* https://gist.github.com/renoirb/b23b2d60fbe1ca25950005bb4056458d#gistcomment-3012183
*/
const path = require("path");
const fs = require("fs");
// For example, ignore files we want in a package, where we can sync.
// where there is a monorepo file called '.prettierignore' in a module
// we know we maintain, and we want that to be synced in the monorepo root.
const filesToCopyToMonorepoRoot = ['conventions/config-prettier/.prettierignore']
/**
* Copy-pasta/butchery from install-run.js
* ************************ Is there a better way? ************************ *
*/
let _rushJsonFolder;
function findRushJsonFolder() {
if (!_rushJsonFolder) {
let basePath = __dirname;
let tempPath = __dirname;
do {
const testRushJsonPath = path.join(basePath, 'rush.json');
if (fs.existsSync(testRushJsonPath)) {
_rushJsonFolder = basePath;
break;
}
else {
basePath = tempPath;
}
} while (basePath !== (tempPath = path.dirname(basePath))); // Exit the loop when we hit the disk root
if (!_rushJsonFolder) {
throw new Error('Unable to find rush.json.');
}
}
return _rushJsonFolder;
}
function getNodeModulesPath(rushJsonDir) {
const packageInstallFolder = path.normalize(path.join(rushJsonDir, 'common', 'temp'))
try {
const nodeModulesFolder = path.resolve(packageInstallFolder, 'node_modules');
const exists = fs.existsSync(nodeModulesFolder)
if (exists) {
return nodeModulesFolder;
}
throw new Error(`node_modules not found`);
}
catch (e) {
throw new Error(`Error finding the node_modules folder (${packageInstallFolder}): ${e}`);
}
}
/** ************************ /Is there a better way? ************************ */
/**
* TODO: Better understand this part.
* Partial copy-pasta from just-scripts, in `src/tasks/__tests__/callTaskForTest.ts`
*/
function wrapTaskFunction(fn, ctx) {
return fn.call(ctx, () => ({/* ??? in which situation does this */}));
}
/** @type {import('just-scripts').CopyTaskOptions} */
const copyTaskOpts = {
// For example, ignore files we want in a package, where we can sync.
paths: [...filesToCopyToMonorepoRoot],
dest: '.'
};
(() => {
const rushFolder = findRushJsonFolder();
const nodeModules = getNodeModulesPath(rushFolder)
const { copyTask, logger } = require(`${nodeModules}/just-scripts`);
// TODO: Improve this.
logger.enableVerbose = true;
const { argv = {} } = process
const context = {
argv: argv || { _: [], $0: '' },
logger
};
// try-catch?
wrapTaskFunction(copyTask(copyTaskOpts), context);
})();
@renoirb
Copy link
Author

renoirb commented Aug 29, 2019

Imagine we have a rush.json file with the following eventHook

{
  "$schema": "https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json",
  "eventHooks": {
    "postRushInstall": [
      // Why is the example does not prepend node. Is it expected to be executable?
      "node common/scripts/post-rush-install.js"
    ],
  }
}

With common/config/rush/common-versions.json

{
  "$schema": "https://developer.microsoft.com/json-schemas/rush/v5/common-versions.schema.json",
  "preferredVersions": {
    "just-scripts": "^0.28.1"
  },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment