Ghost/core/server/lib/fs/package-json/read.js
Hannah Wolfe 22e13acd65 Updated var declarations to const/let and no lists
- All var declarations are now const or let as per ES6
- All comma-separated lists / chained declarations are now one declaration per line
- This is for clarity/readability but also made running the var-to-const/let switch smoother
- ESLint rules updated to match

How this was done:

- npm install -g jscodeshift
- git clone https://github.com/cpojer/js-codemod.git
- git clone git@github.com:TryGhost/Ghost.git shallow-ghost
- cd shallow-ghost
- jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2
- jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2
- yarn
- yarn test
- yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode
- grunt test-regression
- sorted!
2020-04-29 16:51:13 +01:00

91 lines
2.9 KiB
JavaScript

/**
* Dependencies
*/
const Promise = require('bluebird');
const _ = require('lodash');
const join = require('path').join;
const fs = require('fs-extra');
const parsePackageJson = require('./parse');
const common = require('../../common');
const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i;
const packageJSONPath = 'package.json';
let readPackage;
let readPackages;
let processPackage;
/**
* Recursively read directory and find the packages in it
*/
processPackage = function processPackage(absolutePath, packageName) {
const pkg = {
name: packageName,
path: absolutePath
};
return parsePackageJson(join(absolutePath, packageJSONPath))
.then(function gotPackageJSON(packageJSON) {
pkg['package.json'] = packageJSON;
return pkg;
})
.catch(function noPackageJSON() {
// ignore invalid package.json for now,
// because Ghost does not rely/use them at the moment
// in the future, this .catch() will need to be removed,
// so that error is thrown on invalid json syntax
pkg['package.json'] = null;
return pkg;
});
};
readPackage = function readPackage(packagePath, packageName) {
const absolutePath = join(packagePath, packageName);
return fs.stat(absolutePath)
.then(function (stat) {
if (!stat.isDirectory()) {
return {};
}
return processPackage(absolutePath, packageName)
.then(function gotPackage(pkg) {
const res = {};
res[packageName] = pkg;
return res;
});
})
.catch(function (err) {
return Promise.reject(new common.errors.NotFoundError({
message: 'Package not found',
err: err,
help: 'path: ' + packagePath,
context: 'name: ' + packageName
}));
});
};
readPackages = function readPackages(packagePath) {
return fs.readdir(packagePath)
.filter(function (packageName) {
// Filter out things which are not packages by regex
if (packageName.match(notAPackageRegex)) {
return;
}
// Check the remaining items to ensure they are a directory
return fs.stat(join(packagePath, packageName)).then(function (stat) {
return stat.isDirectory();
});
})
.map(function readPackageJson(packageName) {
const absolutePath = join(packagePath, packageName);
return processPackage(absolutePath, packageName);
})
.then(function (packages) {
return _.keyBy(packages, 'name');
});
};
/**
* Expose Public API
*/
module.exports.all = readPackages;
module.exports.one = readPackage;