Skip to content

Commit

Permalink
Chore(repo): Introduce Yarn plugin for overriding state
Browse files Browse the repository at this point in the history
  * this should solve the problem with non-existing node modules
    state file
  * @see: heroku/heroku-buildpack-nodejs#907
  • Loading branch information
literat committed Jul 19, 2024
1 parent 3edd7de commit 2a51b33
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .yarn/plugins/plugin-override-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const ALLOWED_SCRIPTS=["build"];

const NODE_MODULES="node_modules";
const STATE_FILE=".yarn-state.yml";

module.exports = {
name: `plugin-override-state`,
factory: require => ({
hooks: {
async setupScriptEnvironment(project, scriptEnv) {
if (scriptEnv != null && ALLOWED_SCRIPTS.find(script => script == scriptEnv.npm_lifecycle_event) != null) {
if (project.configuration.get(`nodeLinker`) === `node-modules`) {
const stateFile = [project.cwd, NODE_MODULES, STATE_FILE].join("/");
const fs = require("fs");

if (!fs.existsSync(stateFile)) {
console.log("Detected command allowed in stateless environment and state file is missing.");
console.log("Generating empty state file...");

fs.mkdirSync(require('path').dirname(stateFile), { recursive: true });
fs.appendFileSync(stateFile, "# Autogenerated\n", {"flag":"w+"});
fs.appendFileSync(stateFile, "__metadata:\n");
fs.appendFileSync(stateFile, " version: 1\n");
fs.appendFileSync(stateFile, " nmMode: classic\n");
}
}
}
}
},
})
};
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ nodeLinker: node-modules
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-engines.cjs
spec: 'https://raw.githubusercontent.com/devoto13/yarn-plugin-engines/main/bundles/%40yarnpkg/plugin-engines.js'
- .yarn/plugins/plugin-override-state.js

0 comments on commit 2a51b33

Please sign in to comment.