Michael Bradley, Jr 3693ebd90d fix: ensure that packages properly specify their dependencies
Many packages in the monorepo did not specify all of their dependencies; they
were effectively relying on resolution in the monorepo's root
`node_modules`. In a production release of `embark` and `embark[js]-*` packages
this can lead to broken packages.

To fix the problem currently and to help prevent it from happening again, make
use of the `eslint-plugin-import` package's `import/no-extraneous-dependencies`
and `import/no-unresolved` rules. In the root `tslint.json` set
`"no-implicit-dependencies": true`, wich is the tslint equivalent of
`import/no-extraneous-dependencies`; there is no tslint equivalent for
`import/no-unresolved`, but we will eventually replace tslint with an eslint
configuration that checks both `.js` and `.ts` files.

For `import/no-unresolved` to work in our monorepo setup, in most packages add
an `index.js` that has:

```js
module.exports = require('./dist'); // or './dist/lib' in some cases
```

And point `"main"` in `package.json` to `"./index.js"`. Despite what's
indicated in npm's documentation for `package.json`, it's also necessary to add
`"index.js"` to the `"files"` array.

Make sure that all `.js` files that can and should be linted are in fact
linted. For example, files in `packages/embark/src/cmd/` weren't being linted
and many test suites weren't being linted.

Bump all relevant packages to `eslint@6.8.0`.

Fix all linter errors that arose after these changes.

Implement a `check-yarn-lock` script that's run as part of `"ci:full"` and
`"qa:full"`, and can manually be invoked via `yarn cylock` in the root of the
monorepo. The script exits with error if any specifiers are found in
`yarn.lock` for `embark[js][-*]` and/or `@embarklabs/*` (with a few exceptions,
cf. `scripts/check-yarn-lock.js`).
2020-02-25 14:52:10 -06:00

153 lines
4.4 KiB
JavaScript

const fs = require('fs-extra');
const os = require('os');
const parseJson = require('parse-json');
const path = require('path');
import { dappPath, embarkPath, joinPath, pkgPath } from 'embark-utils';
require('colors');
function restrictPath(receiver, binding, count, args) {
const dapp = dappPath();
let embark = embarkPath();
const pkg = pkgPath();
// In the monorepo, enable doing FS functions on all of embark (needed to access embark/node_modules)
embark = embark.replace(path.normalize('embark/packages/'), '');
const allowedRoots = [
dapp,
embark,
pkg,
os.tmpdir()
];
let allInsideRestricted = true;
for (let i = 0; i < count; i++) {
const resolved = path.resolve(dapp, args[i]);
allInsideRestricted = allowedRoots.some(p => { return resolved.indexOf(p) === 0; });
if (!allInsideRestricted) break;
}
if (allInsideRestricted) return receiver.apply(binding, args);
throw new Error('EPERM: Operation not permitted');
}
function mkdirpSync(...args) { return restrictPath(fs.mkdirpSync, fs, 1, args); }
function mkdirp(...args) { return restrictPath(fs.mkdirp, fs, 1, args); }
function readdir(...args) { return restrictPath(fs.readdir, fs, 1, args); }
function stat(...args) { return restrictPath(fs.stat, fs, 1, args); }
function remove(...args) { return restrictPath(fs.remove, fs, 1, args); }
function copy(...args) { return restrictPath(fs.copy, fs, 2, args); }
function copySync(...args) { return restrictPath(fs.copySync, fs, 2, args); }
function move(...args) { return restrictPath(fs.move, fs, 2, args); }
function moveSync(...args) { return restrictPath(fs.moveSync, fs, 2, args); }
function symlink(...args) { return restrictPath(fs.symlink, fs, 2, args); }
function appendFileSync(...args) { return restrictPath(fs.appendFileSync, fs, 1, args); }
function writeFile(...args) { return restrictPath(fs.writeFile, fs, 1, args); }
function writeFileSync(...args) { return restrictPath(fs.writeFileSync, fs, 1, args); }
function readFile(...args) { return restrictPath(fs.readFile, fs, 1, args); }
function readFileSync(...args) { return restrictPath(fs.readFileSync, fs, 1, args); }
function readdirSync(...args) { return restrictPath(fs.readdirSync, fs, 1, args); }
function statSync(...args) { return restrictPath(fs.statSync, fs, 1, args); }
function readJSONSync(...args) {
const content = readFileSync(...args);
let json;
try {
json = parseJson(content);
} catch(e) {
console.error('error: '.red + args[0].green.underline + ' ' + e.message.green);
process.exit(0);
}
return json;
}
function writeJSONSync(...args) { return restrictPath(fs.writeJSONSync, fs, 1, args); }
function outputJSONSync(...args) { return restrictPath(fs.outputJSONSync, fs, 1, args); }
function writeJson(...args) { return restrictPath(fs.writeJson, fs, 1, args); }
function existsSync(...args) { return restrictPath(fs.existsSync, fs, 1, args); }
function ensureFileSync(...args) { return restrictPath(fs.ensureFileSync, fs, 1, args); }
function ensureDirSync(...args) { return restrictPath(fs.ensureDirSync, fs, 1, args); }
function access(...args) { return restrictPath(fs.access, fs, 1, args); }
function removeSync(...args) { return restrictPath(fs.removeSync, fs, 1, args); }
function createWriteStream(...args) { return restrictPath(fs.createWriteStream, fs, 1, args); }
function copyPreserve(sourceFilePath, targetFilePath) {
const implementation = (sourceFilePath, targetFilePath) => {
let ext = 1;
let preserved = targetFilePath;
while (fs.existsSync(preserved)) {
const extname = path.extname(targetFilePath);
preserved = joinPath(
path.dirname(targetFilePath),
`${path.basename(targetFilePath, extname)}.${ext}${extname}`
);
ext++;
}
if (preserved !== targetFilePath) {
fs.copySync(targetFilePath, preserved);
}
fs.copySync(sourceFilePath, targetFilePath);
};
return restrictPath(implementation, {}, 2, [sourceFilePath, targetFilePath]);
}
function outputFileSync(...args) { return restrictPath(fs.outputFileSync, fs, 1, args); }
module.exports = {
access,
appendFileSync,
copy,
copyPreserve,
copySync,
createWriteStream,
existsSync,
ensureFileSync,
ensureDirSync,
mkdirp,
mkdirpSync,
move,
moveSync,
outputFileSync,
outputJSONSync,
readFile,
readFileSync,
readJSONSync,
readdir,
readdirSync,
remove,
removeSync,
stat,
statSync,
symlink,
writeFile,
writeFileSync,
writeJSONSync,
writeJson
};