From 69c1e2647dd55f46a9fc5910bd17e863886a254e Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Thu, 14 Dec 2017 22:07:53 +0100 Subject: [PATCH 01/27] Moved zip folder, read csv and package-json to lib/fs refs #9178, refs https://github.com/TryGhost/Ghost/commit/849e97640ff507ebbe3464ccf7ae0049593cdb6f - i've reconsidered, these modules belong to lib - prettify package-json module --- ghost/package-json/lib/filter.js | 44 +++++++++++++++ ghost/package-json/lib/index.js | 27 ++++++++++ ghost/package-json/lib/parse.js | 53 +++++++++++++++++++ ghost/package-json/lib/read.js | 91 ++++++++++++++++++++++++++++++++ 4 files changed, 215 insertions(+) create mode 100644 ghost/package-json/lib/filter.js create mode 100644 ghost/package-json/lib/index.js create mode 100644 ghost/package-json/lib/parse.js create mode 100644 ghost/package-json/lib/read.js diff --git a/ghost/package-json/lib/filter.js b/ghost/package-json/lib/filter.js new file mode 100644 index 0000000000..d9ac182049 --- /dev/null +++ b/ghost/package-json/lib/filter.js @@ -0,0 +1,44 @@ +var _ = require('lodash'), + notAPackageRegex = /^\.|_messages|README.md/i, + filterPackages; + +/** + * ### Filter Packages + * Normalizes packages read by read-packages so that the apps and themes modules can use them. + * Iterates over each package and return an array of objects which are simplified representations of the package + * with 3 properties: + * - `name` - the package name + * - `package` - contents of the package.json or false if there isn't one + * - `active` - set to true if this package is active + * This data structure is used for listings of packages provided over the API and as such + * deliberately combines multiple sources of information in order to be efficient. + * + * TODO: simplify the package.json representation to contain only fields we use + * + * @param {object} packages as returned by read-packages + * @param {array/string} active as read from the settings object + * @returns {Array} of objects with useful info about apps / themes + */ +filterPackages = function filterPackages(packages, active) { + // turn active into an array (so themes and apps can be checked the same) + if (!Array.isArray(active)) { + active = [active]; + } + + return _.reduce(packages, function (result, pkg, key) { + var item = {}; + if (!key.match(notAPackageRegex)) { + item = { + name: key, + package: pkg['package.json'] || false, + active: _.indexOf(active, key) !== -1 + }; + + result.push(item); + } + + return result; + }, []); +}; + +module.exports = filterPackages; diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js new file mode 100644 index 0000000000..354949ce23 --- /dev/null +++ b/ghost/package-json/lib/index.js @@ -0,0 +1,27 @@ +/** + * # Package Utils + * + * Ghost has / is in the process of gaining support for several different types of sub-packages: + * - Themes: have always been packages, but we're going to lean more heavily on npm & package.json in future + * - Adapters: an early version of apps, replace fundamental pieces like storage, will become npm modules + * - Apps: plugins that can be installed whilst Ghost is running & modify behaviour + * - More? + * + * These utils facilitate loading, reading, managing etc, packages from the file system. + */ + +'use strict'; + +module.exports = { + get read() { + return require('./read'); + }, + + get parse() { + return require('./parse'); + }, + + get filter() { + return require('./filter'); + } +}; diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js new file mode 100644 index 0000000000..eb76276f21 --- /dev/null +++ b/ghost/package-json/lib/parse.js @@ -0,0 +1,53 @@ +/** + * Dependencies + */ + +var Promise = require('bluebird'), + fs = require('fs-extra'), + common = require('../../common'); + +/** + * Parse package.json and validate it has + * all the required fields + */ + +function parsePackageJson(path) { + return fs.readFile(path) + .catch(function () { + var err = new Error(common.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); + err.context = path; + + return Promise.reject(err); + }) + .then(function (source) { + var hasRequiredKeys, json, err; + + try { + json = JSON.parse(source); + + hasRequiredKeys = json.name && json.version; + + if (!hasRequiredKeys) { + err = new Error(common.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); + err.context = path; + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://docs.ghost.org/themes/'}); + + return Promise.reject(err); + } + + return json; + } catch (parseError) { + err = new Error(common.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); + err.context = path; + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://docs.ghost.org/themes/'}); + + return Promise.reject(err); + } + }); +} + +/** + * Expose `parsePackageJson` + */ + +module.exports = parsePackageJson; diff --git a/ghost/package-json/lib/read.js b/ghost/package-json/lib/read.js new file mode 100644 index 0000000000..6a10fab547 --- /dev/null +++ b/ghost/package-json/lib/read.js @@ -0,0 +1,91 @@ +/** + * Dependencies + */ +var Promise = require('bluebird'), + _ = require('lodash'), + join = require('path').join, + fs = require('fs-extra'), + parsePackageJson = require('./parse'), + common = require('../../common'), + + notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i, + packageJSONPath = 'package.json', + + readPackage, + readPackages, + processPackage; + +/** + * Recursively read directory and find the packages in it + */ +processPackage = function processPackage(absolutePath, packageName) { + var 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) { + var absolutePath = join(packagePath, packageName); + return fs.stat(absolutePath) + .then(function (stat) { + if (!stat.isDirectory()) { + return {}; + } + + return processPackage(absolutePath, packageName) + .then(function gotPackage(pkg) { + var 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) { + var 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; From 821db41992cdd5637139b0759395ac2da6df5fd0 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Mon, 30 Apr 2018 12:33:36 +0200 Subject: [PATCH 02/27] =?UTF-8?q?=F0=9F=94=A5=20=20Drop=20Node=20v4=20Supp?= =?UTF-8?q?ort?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no issue - support ends today - see https://github.com/nodejs/Release - removed `use strict` --- ghost/package-json/lib/index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js index 354949ce23..0f5a4c1bc5 100644 --- a/ghost/package-json/lib/index.js +++ b/ghost/package-json/lib/index.js @@ -10,8 +10,6 @@ * These utils facilitate loading, reading, managing etc, packages from the file system. */ -'use strict'; - module.exports = { get read() { return require('./read'); From 847d4b2f013dca0026145ea0f8196821a3702e40 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Fri, 20 Jul 2018 23:33:41 +0200 Subject: [PATCH 03/27] Updated docs links refs #9742 - Ghost 2.0 is coming - all doc links in 1.0 must use concrete links e.g. docs.ghost.org/v1 or themes.ghost.org/v1.23.0/ - if we release Ghost 2.0, docs.ghost.org will show 2.0 docs --- ghost/package-json/lib/parse.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js index eb76276f21..3271afc091 100644 --- a/ghost/package-json/lib/parse.js +++ b/ghost/package-json/lib/parse.js @@ -30,7 +30,7 @@ function parsePackageJson(path) { if (!hasRequiredKeys) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://docs.ghost.org/themes/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://themes.ghost.org/v1.23.0/'}); return Promise.reject(err); } @@ -39,7 +39,7 @@ function parsePackageJson(path) { } catch (parseError) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://docs.ghost.org/themes/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://themes.ghost.org/v1.23.0/'}); return Promise.reject(err); } From cb03ca252953b9914e5104128229b2717cffbdc9 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Fri, 20 Jul 2018 23:42:39 +0200 Subject: [PATCH 04/27] Changed http to https links no issue - use https - replace broken links e.g. docs.ghost.org/themes --- ghost/package-json/lib/parse.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js index 3271afc091..0766c21da0 100644 --- a/ghost/package-json/lib/parse.js +++ b/ghost/package-json/lib/parse.js @@ -30,7 +30,7 @@ function parsePackageJson(path) { if (!hasRequiredKeys) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://themes.ghost.org/v1.23.0/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://themes.ghost.org/'}); return Promise.reject(err); } @@ -39,7 +39,7 @@ function parsePackageJson(path) { } catch (parseError) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'http://themes.ghost.org/v1.23.0/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://themes.ghost.org/'}); return Promise.reject(err); } From 9ca92908ef9a66aa57ea8470cd590300c1f5d7c4 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 17 Jan 2019 06:57:37 +0000 Subject: [PATCH 05/27] Updated docs links to best equivalents (#10386) * Updated docs links to best equivalents - Our documentation has been overhauled, this updates the all the old links sprinkled through Ghost * Update integrity hash --- ghost/package-json/lib/parse.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js index 0766c21da0..7134708900 100644 --- a/ghost/package-json/lib/parse.js +++ b/ghost/package-json/lib/parse.js @@ -30,7 +30,7 @@ function parsePackageJson(path) { if (!hasRequiredKeys) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://themes.ghost.org/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://docs.ghost.org/api/handlebars-themes/'}); return Promise.reject(err); } @@ -39,7 +39,7 @@ function parsePackageJson(path) { } catch (parseError) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://themes.ghost.org/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://docs.ghost.org/api/handlebars-themes/'}); return Promise.reject(err); } From 53ef388321ef9791166b5aedef1bc1e3778112f2 Mon Sep 17 00:00:00 2001 From: Aileen Nowak Date: Mon, 22 Jul 2019 18:17:50 +0800 Subject: [PATCH 06/27] Updated links to docs (#10941) no issue --- ghost/package-json/lib/parse.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js index 7134708900..47b55b50f1 100644 --- a/ghost/package-json/lib/parse.js +++ b/ghost/package-json/lib/parse.js @@ -30,7 +30,7 @@ function parsePackageJson(path) { if (!hasRequiredKeys) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://docs.ghost.org/api/handlebars-themes/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/v2/handlebars-themes/'}); return Promise.reject(err); } @@ -39,7 +39,7 @@ function parsePackageJson(path) { } catch (parseError) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://docs.ghost.org/api/handlebars-themes/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/v2/handlebars-themes/'}); return Promise.reject(err); } From 14e5ae93b3ba21e6f4c1a379164b20608f7cc1c2 Mon Sep 17 00:00:00 2001 From: Aileen Nowak Date: Thu, 25 Jul 2019 15:17:23 +0800 Subject: [PATCH 07/27] Updated docs api links to be version-less --- ghost/package-json/lib/parse.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js index 47b55b50f1..7b0454bf9e 100644 --- a/ghost/package-json/lib/parse.js +++ b/ghost/package-json/lib/parse.js @@ -30,7 +30,7 @@ function parsePackageJson(path) { if (!hasRequiredKeys) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/v2/handlebars-themes/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); return Promise.reject(err); } @@ -39,7 +39,7 @@ function parsePackageJson(path) { } catch (parseError) { err = new Error(common.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/v2/handlebars-themes/'}); + err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); return Promise.reject(err); } From 20507b5dd5d7e697898e3e820a47020b05aaf38e Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 19 Mar 2020 14:58:59 +0000 Subject: [PATCH 08/27] Remove Apps - Apps are marked as removed in 3.0, never officially launched and have been deprecated for at least 2 years. - We've slowly removed bits that got in our way or were insecure over time meaning they mostly didn't work - This cleans up the remainder of the logic - The tables should be cleaned up in a future major --- ghost/package-json/lib/filter.js | 6 +++--- ghost/package-json/lib/index.js | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/ghost/package-json/lib/filter.js b/ghost/package-json/lib/filter.js index d9ac182049..c461599514 100644 --- a/ghost/package-json/lib/filter.js +++ b/ghost/package-json/lib/filter.js @@ -4,7 +4,7 @@ var _ = require('lodash'), /** * ### Filter Packages - * Normalizes packages read by read-packages so that the apps and themes modules can use them. + * Normalizes packages read by read-packages so that the themes module can use them. * Iterates over each package and return an array of objects which are simplified representations of the package * with 3 properties: * - `name` - the package name @@ -17,10 +17,10 @@ var _ = require('lodash'), * * @param {object} packages as returned by read-packages * @param {array/string} active as read from the settings object - * @returns {Array} of objects with useful info about apps / themes + * @returns {Array} of objects with useful info about themes */ filterPackages = function filterPackages(packages, active) { - // turn active into an array (so themes and apps can be checked the same) + // turn active into an array if it isn't one, so this function can deal with lists and one-offs if (!Array.isArray(active)) { active = [active]; } diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js index 0f5a4c1bc5..cbc3cb022f 100644 --- a/ghost/package-json/lib/index.js +++ b/ghost/package-json/lib/index.js @@ -3,9 +3,7 @@ * * Ghost has / is in the process of gaining support for several different types of sub-packages: * - Themes: have always been packages, but we're going to lean more heavily on npm & package.json in future - * - Adapters: an early version of apps, replace fundamental pieces like storage, will become npm modules - * - Apps: plugins that can be installed whilst Ghost is running & modify behaviour - * - More? + * - Adapters: replace fundamental pieces like storage, will become npm modules * * These utils facilitate loading, reading, managing etc, packages from the file system. */ From 8ccf7fd172cc22341a9505b6ebed6f812d2ab849 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Fri, 20 Mar 2020 08:58:26 +0000 Subject: [PATCH 09/27] Revert "Remove Apps" This reverts commit 20507b5dd5d7e697898e3e820a47020b05aaf38e. --- ghost/package-json/lib/filter.js | 6 +++--- ghost/package-json/lib/index.js | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/ghost/package-json/lib/filter.js b/ghost/package-json/lib/filter.js index c461599514..d9ac182049 100644 --- a/ghost/package-json/lib/filter.js +++ b/ghost/package-json/lib/filter.js @@ -4,7 +4,7 @@ var _ = require('lodash'), /** * ### Filter Packages - * Normalizes packages read by read-packages so that the themes module can use them. + * Normalizes packages read by read-packages so that the apps and themes modules can use them. * Iterates over each package and return an array of objects which are simplified representations of the package * with 3 properties: * - `name` - the package name @@ -17,10 +17,10 @@ var _ = require('lodash'), * * @param {object} packages as returned by read-packages * @param {array/string} active as read from the settings object - * @returns {Array} of objects with useful info about themes + * @returns {Array} of objects with useful info about apps / themes */ filterPackages = function filterPackages(packages, active) { - // turn active into an array if it isn't one, so this function can deal with lists and one-offs + // turn active into an array (so themes and apps can be checked the same) if (!Array.isArray(active)) { active = [active]; } diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js index cbc3cb022f..0f5a4c1bc5 100644 --- a/ghost/package-json/lib/index.js +++ b/ghost/package-json/lib/index.js @@ -3,7 +3,9 @@ * * Ghost has / is in the process of gaining support for several different types of sub-packages: * - Themes: have always been packages, but we're going to lean more heavily on npm & package.json in future - * - Adapters: replace fundamental pieces like storage, will become npm modules + * - Adapters: an early version of apps, replace fundamental pieces like storage, will become npm modules + * - Apps: plugins that can be installed whilst Ghost is running & modify behaviour + * - More? * * These utils facilitate loading, reading, managing etc, packages from the file system. */ From 134e069c1ba290ecaa1db14230cff0698708d6b0 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 19 Mar 2020 15:23:10 +0000 Subject: [PATCH 10/27] Remove External Apps - Apps are marked as removed in 3.0, never officially launched and have been deprecated for at least 2 years. - We've slowly removed bits that got in our way or were insecure over time meaning they mostly didn't work - This cleans up the remainder of the logic - The tables should be cleaned up in a future major --- ghost/package-json/lib/filter.js | 6 +++--- ghost/package-json/lib/index.js | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/ghost/package-json/lib/filter.js b/ghost/package-json/lib/filter.js index d9ac182049..c461599514 100644 --- a/ghost/package-json/lib/filter.js +++ b/ghost/package-json/lib/filter.js @@ -4,7 +4,7 @@ var _ = require('lodash'), /** * ### Filter Packages - * Normalizes packages read by read-packages so that the apps and themes modules can use them. + * Normalizes packages read by read-packages so that the themes module can use them. * Iterates over each package and return an array of objects which are simplified representations of the package * with 3 properties: * - `name` - the package name @@ -17,10 +17,10 @@ var _ = require('lodash'), * * @param {object} packages as returned by read-packages * @param {array/string} active as read from the settings object - * @returns {Array} of objects with useful info about apps / themes + * @returns {Array} of objects with useful info about themes */ filterPackages = function filterPackages(packages, active) { - // turn active into an array (so themes and apps can be checked the same) + // turn active into an array if it isn't one, so this function can deal with lists and one-offs if (!Array.isArray(active)) { active = [active]; } diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js index 0f5a4c1bc5..cbc3cb022f 100644 --- a/ghost/package-json/lib/index.js +++ b/ghost/package-json/lib/index.js @@ -3,9 +3,7 @@ * * Ghost has / is in the process of gaining support for several different types of sub-packages: * - Themes: have always been packages, but we're going to lean more heavily on npm & package.json in future - * - Adapters: an early version of apps, replace fundamental pieces like storage, will become npm modules - * - Apps: plugins that can be installed whilst Ghost is running & modify behaviour - * - More? + * - Adapters: replace fundamental pieces like storage, will become npm modules * * These utils facilitate loading, reading, managing etc, packages from the file system. */ From d47df7ad46c2cc43f2e91f09322ceb04be4f96dd Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 30 Mar 2020 16:26:47 +0100 Subject: [PATCH 11/27] Move tests from core to root (#11700) - move all test files from core/test to test/ - updated all imports and other references - all code inside of core/ is then application code - tests are correctly at the root level - consistent with other repos/projects Co-authored-by: Kevin Ansfield --- ghost/package-json/test/filter_spec.js | 122 ++++++++++++ ghost/package-json/test/parse_spec.js | 120 ++++++++++++ ghost/package-json/test/read_spec.js | 259 +++++++++++++++++++++++++ 3 files changed, 501 insertions(+) create mode 100644 ghost/package-json/test/filter_spec.js create mode 100644 ghost/package-json/test/parse_spec.js create mode 100644 ghost/package-json/test/read_spec.js diff --git a/ghost/package-json/test/filter_spec.js b/ghost/package-json/test/filter_spec.js new file mode 100644 index 0000000000..3c7f3358d2 --- /dev/null +++ b/ghost/package-json/test/filter_spec.js @@ -0,0 +1,122 @@ +var should = require('should'), + packageJSON = require('../../../../../core/server/lib/fs/package-json'); + +describe('lib/fs/package-json', function () { + // @TODO: introduce some non-theme package examples + var casper = { + name: 'casper', + path: '~/content/themes/casper', + 'package.json': { + name: 'casper', + description: 'The default personal blogging theme for Ghost. Beautiful, minimal and responsive.', + demo: 'https://demo.ghost.io', + version: '1.3.5', + engines: {}, + license: 'MIT', + screenshots: {}, + author: {}, + gpm: {}, + keywords: {}, + repository: {}, + bugs: 'https://github.com/TryGhost/Casper/issues', + contributors: 'https://github.com/TryGhost/Casper/graphs/contributors' + } + }, + simplePackage = { + name: 'simple', + path: '~/content/themes/simple', + 'package.json': { + name: 'simple', + version: '0.1.0' + } + }, + missingPackageJson = { + name: 'missing', + path: '~/content/themes/missing', + 'package.json': null + }; + + it('should filter packages correctly', function () { + var result = packageJSON.filter({casper: casper}), + package1; + + result.should.be.an.Array().with.lengthOf(1); + package1 = result[0]; + + package1.should.be.an.Object().with.properties('name', 'package', 'active'); + Object.keys(package1).should.be.an.Array().with.lengthOf(3); + package1.name.should.eql('casper'); + package1.package.should.be.an.Object().with.properties('name', 'version'); + package1.active.should.be.false(); + }); + + it('should filter packages and handle a single active package string', function () { + var result = packageJSON.filter({casper: casper, simple: simplePackage}, 'casper'), + package1, package2; + + result.should.be.an.Array().with.lengthOf(2); + package1 = result[0]; + package2 = result[1]; + + package1.should.be.an.Object().with.properties('name', 'package', 'active'); + Object.keys(package1).should.be.an.Array().with.lengthOf(3); + package1.name.should.eql('casper'); + package1.package.should.be.an.Object().with.properties('name', 'version'); + package1.active.should.be.true(); + + package2.should.be.an.Object().with.properties('name', 'package', 'active'); + Object.keys(package2).should.be.an.Array().with.lengthOf(3); + package2.name.should.eql('simple'); + package2.package.should.be.an.Object().with.properties('name', 'version'); + package2.active.should.be.false(); + }); + + it('should filter packages and handle an array of active packages', function () { + var result = packageJSON.filter({casper: casper, simple: simplePackage}, ['casper', 'simple']), + package1, package2; + + result.should.be.an.Array().with.lengthOf(2); + package1 = result[0]; + package2 = result[1]; + + package1.should.be.an.Object().with.properties('name', 'package', 'active'); + Object.keys(package1).should.be.an.Array().with.lengthOf(3); + package1.name.should.eql('casper'); + package1.package.should.be.an.Object().with.properties('name', 'version'); + package1.active.should.be.true(); + + package2.should.be.an.Object().with.properties('name', 'package', 'active'); + Object.keys(package2).should.be.an.Array().with.lengthOf(3); + package2.name.should.eql('simple'); + package2.package.should.be.an.Object().with.properties('name', 'version'); + package2.active.should.be.true(); + }); + + it('handles packages with no package.json even though this makes us sad', function () { + var result = packageJSON.filter({casper: casper, missing: missingPackageJson}, ['casper']), + package1, package2; + + result.should.be.an.Array().with.lengthOf(2); + package1 = result[0]; + package2 = result[1]; + + package1.should.be.an.Object().with.properties('name', 'package', 'active'); + Object.keys(package1).should.be.an.Array().with.lengthOf(3); + package1.name.should.eql('casper'); + package1.package.should.be.an.Object().with.properties('name', 'version'); + package1.active.should.be.true(); + + package2.should.be.an.Object().with.properties('name', 'package', 'active'); + Object.keys(package2).should.be.an.Array().with.lengthOf(3); + package2.name.should.eql('missing'); + package2.package.should.be.false(); + package2.active.should.be.false(); + }); + + it('filters out things which are not packages', function () { + var result = packageJSON.filter({ + '.git': {}, '.anything': {}, 'README.md': {}, _messages: {} + }); + result.should.be.an.Array().with.lengthOf(0); + }); +}); diff --git a/ghost/package-json/test/parse_spec.js b/ghost/package-json/test/parse_spec.js new file mode 100644 index 0000000000..40212eef03 --- /dev/null +++ b/ghost/package-json/test/parse_spec.js @@ -0,0 +1,120 @@ +var should = require('should'), + tmp = require('tmp'), + fs = require('fs-extra'), + packageJSON = require('../../../../../core/server/lib/fs/package-json'); + +describe('lib/fs/package-json: parse', function () { + it('should parse valid package.json', function (done) { + var pkgJson, tmpFile; + + tmpFile = tmp.fileSync(); + pkgJson = JSON.stringify({ + name: 'test', + version: '0.0.0' + }); + + fs.writeSync(tmpFile.fd, pkgJson); + + packageJSON.parse(tmpFile.name) + .then(function (pkg) { + pkg.should.eql({ + name: 'test', + version: '0.0.0' + }); + + done(); + }) + .catch(done) + .finally(tmpFile.removeCallback); + }); + + it('should fail when name is missing', function (done) { + var pkgJson, tmpFile; + + tmpFile = tmp.fileSync(); + pkgJson = JSON.stringify({ + version: '0.0.0' + }); + + fs.writeSync(tmpFile.fd, pkgJson); + + packageJSON.parse(tmpFile.name) + .then(function () { + done(new Error('packageJSON.parse succeeded, but should\'ve failed')); + }) + .catch(function (err) { + err.message.should.equal('"name" or "version" is missing from theme package.json file.'); + err.context.should.equal(tmpFile.name); + err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/'); + + done(); + }) + .catch(done) + .finally(tmpFile.removeCallback); + }); + + it('should fail when version is missing', function (done) { + var pkgJson, tmpFile; + + tmpFile = tmp.fileSync(); + pkgJson = JSON.stringify({ + name: 'test' + }); + + fs.writeSync(tmpFile.fd, pkgJson); + + packageJSON.parse(tmpFile.name) + .then(function () { + done(new Error('packageJSON.parse succeeded, but should\'ve failed')); + }) + .catch(function (err) { + err.message.should.equal('"name" or "version" is missing from theme package.json file.'); + err.context.should.equal(tmpFile.name); + err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/'); + + done(); + }) + .catch(done) + .finally(tmpFile.removeCallback); + }); + + it('should fail when JSON is invalid', function (done) { + var pkgJson, tmpFile; + + tmpFile = tmp.fileSync(); + pkgJson = '{name:"test"}'; + + fs.writeSync(tmpFile.fd, pkgJson); + + packageJSON.parse(tmpFile.name) + .then(function () { + done(new Error('packageJSON.parse succeeded, but should\'ve failed')); + }) + .catch(function (err) { + err.message.should.equal('Theme package.json file is malformed'); + err.context.should.equal(tmpFile.name); + err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/'); + + done(); + }) + .catch(done) + .finally(tmpFile.removeCallback); + }); + + it('should fail when file is missing', function (done) { + var tmpFile = tmp.fileSync(); + + tmpFile.removeCallback(); + packageJSON.parse(tmpFile.name) + .then(function () { + done(new Error('packageJSON.parse succeeded, but should\'ve failed')); + }) + .catch(function (err) { + err.message.should.equal('Could not read package.json file'); + err.context.should.equal(tmpFile.name); + + done(); + }) + .catch(done); + }); +}); diff --git a/ghost/package-json/test/read_spec.js b/ghost/package-json/test/read_spec.js new file mode 100644 index 0000000000..8dd25296d1 --- /dev/null +++ b/ghost/package-json/test/read_spec.js @@ -0,0 +1,259 @@ +var should = require('should'), + tmp = require('tmp'), + join = require('path').join, + fs = require('fs-extra'), + packageJSON = require('../../../../../core/server/lib/fs/package-json'); + +describe('lib/fs/package-json: read', function () { + describe('all', function () { + it('should read directory and ignore unneeded items', function (done) { + var packagePath = tmp.dirSync({unsafeCleanup: true}); + + // create example theme + fs.mkdirSync(join(packagePath.name, 'casper')); + fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs')); + + // create some trash + fs.mkdirSync(join(packagePath.name, 'node_modules')); + fs.mkdirSync(join(packagePath.name, 'bower_components')); + fs.mkdirSync(join(packagePath.name, '.git')); + fs.writeFileSync(join(packagePath.name, '.DS_Store')); + + packageJSON.read.all(packagePath.name) + .then(function (pkgs) { + pkgs.should.eql({ + casper: { + name: 'casper', + path: join(packagePath.name, 'casper'), + 'package.json': null + } + }); + + done(); + }) + .catch(done) + .finally(packagePath.removeCallback); + }); + + it('should read directory and parse package.json files', function (done) { + var packagePath, pkgJson; + + packagePath = tmp.dirSync({unsafeCleanup: true}); + pkgJson = JSON.stringify({ + name: 'test', + version: '0.0.0' + }); + + // create example theme + fs.mkdirSync(join(packagePath.name, 'testtheme')); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs')); + + packageJSON.read.all(packagePath.name) + .then(function (pkgs) { + pkgs.should.eql({ + testtheme: { + name: 'testtheme', + path: join(packagePath.name, 'testtheme'), + 'package.json': { + name: 'test', + version: '0.0.0' + } + } + }); + + done(); + }) + .catch(done) + .finally(packagePath.removeCallback); + }); + + it('should read directory and ignore invalid package.json files', function (done) { + var packagePath, pkgJson; + + packagePath = tmp.dirSync({unsafeCleanup: true}); + pkgJson = JSON.stringify({ + name: 'test' + }); + + // create example theme + fs.mkdirSync(join(packagePath.name, 'testtheme')); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs')); + + packageJSON.read.all(packagePath.name) + .then(function (pkgs) { + pkgs.should.eql({ + testtheme: { + name: 'testtheme', + path: join(packagePath.name, 'testtheme'), + 'package.json': null + } + }); + + done(); + }) + .catch(done) + .finally(packagePath.removeCallback); + }); + }); + + describe('one', function () { + it('should read directory and ignore unneeded items', function (done) { + var packagePath = tmp.dirSync({unsafeCleanup: true}); + + // create example theme + fs.mkdirSync(join(packagePath.name, 'casper')); + fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs')); + + // create some trash + fs.mkdirSync(join(packagePath.name, 'node_modules')); + fs.mkdirSync(join(packagePath.name, 'bower_components')); + fs.mkdirSync(join(packagePath.name, '.git')); + fs.writeFileSync(join(packagePath.name, '.DS_Store')); + + packageJSON.read.one(packagePath.name, 'casper') + .then(function (pkgs) { + pkgs.should.eql({ + casper: { + name: 'casper', + path: join(packagePath.name, 'casper'), + 'package.json': null + } + }); + + done(); + }) + .catch(done) + .finally(packagePath.removeCallback); + }); + + it('should read directory and parse package.json files', function (done) { + var packagePath, pkgJson; + + packagePath = tmp.dirSync({unsafeCleanup: true}); + pkgJson = JSON.stringify({ + name: 'test', + version: '0.0.0' + }); + + // create example theme + fs.mkdirSync(join(packagePath.name, 'testtheme')); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs')); + + packageJSON.read.one(packagePath.name, 'testtheme') + .then(function (pkgs) { + pkgs.should.eql({ + testtheme: { + name: 'testtheme', + path: join(packagePath.name, 'testtheme'), + 'package.json': { + name: 'test', + version: '0.0.0' + } + } + }); + + done(); + }) + .catch(done) + .finally(packagePath.removeCallback); + }); + + it('should read directory and ignore invalid package.json files', function (done) { + var packagePath, pkgJson; + + packagePath = tmp.dirSync({unsafeCleanup: true}); + pkgJson = JSON.stringify({ + name: 'test' + }); + + // create example theme + fs.mkdirSync(join(packagePath.name, 'testtheme')); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs')); + + packageJSON.read.one(packagePath.name, 'testtheme') + .then(function (pkgs) { + pkgs.should.eql({ + testtheme: { + name: 'testtheme', + path: join(packagePath.name, 'testtheme'), + 'package.json': null + } + }); + + done(); + }) + .catch(done) + .finally(packagePath.removeCallback); + }); + + it('should read directory and include only single requested package', function (done) { + var packagePath = tmp.dirSync({unsafeCleanup: true}); + + // create trash + fs.writeFileSync(join(packagePath.name, 'casper.zip')); + fs.writeFileSync(join(packagePath.name, '.DS_Store')); + + // create actual theme + fs.mkdirSync(join(packagePath.name, 'casper')); + fs.mkdirSync(join(packagePath.name, 'casper', 'partials')); + fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs')); + fs.writeFileSync(join(packagePath.name, 'casper', 'partials', 'navigation.hbs')); + fs.mkdirSync(join(packagePath.name, 'not-casper')); + fs.writeFileSync(join(packagePath.name, 'not-casper', 'index.hbs')); + + packageJSON.read.one(packagePath.name, 'casper') + .then(function (pkgs) { + pkgs.should.eql({ + casper: { + name: 'casper', + path: join(packagePath.name, 'casper'), + 'package.json': null + } + }); + + done(); + }) + .catch(done) + .finally(packagePath.removeCallback); + }); + + it('should return an error if package cannot be found', function (done) { + var packagePath = tmp.dirSync({unsafeCleanup: true}); + + // create trash + fs.writeFileSync(join(packagePath.name, 'casper.zip')); + fs.writeFileSync(join(packagePath.name, '.DS_Store')); + + packageJSON.read.one(packagePath.name, 'casper') + .then(function () { + done('Should have thrown an error'); + }) + .catch(function (err) { + err.message.should.eql('Package not found'); + done(); + }) + .finally(packagePath.removeCallback); + }); + + it('should return empty object if package is not a directory', function (done) { + var packagePath = tmp.dirSync({unsafeCleanup: true}); + + // create trash + fs.writeFileSync(join(packagePath.name, 'casper.zip')); + fs.writeFileSync(join(packagePath.name, '.DS_Store')); + + packageJSON.read.one(packagePath.name, 'casper.zip') + .then(function (pkg) { + pkg.should.eql({}); + + done(); + }) + .catch(done) + .finally(packagePath.removeCallback); + }); + }); +}); From 3db4f8d331e9b973ee1ab5db6e029eb420cb0e53 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Wed, 29 Apr 2020 16:44:27 +0100 Subject: [PATCH 12/27] 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! --- ghost/package-json/lib/filter.js | 8 +-- ghost/package-json/lib/parse.js | 13 ++-- ghost/package-json/lib/read.js | 31 +++++---- ghost/package-json/test/filter_spec.js | 87 ++++++++++++++------------ ghost/package-json/test/parse_spec.js | 22 ++++--- ghost/package-json/test/read_spec.js | 32 +++++----- 6 files changed, 104 insertions(+), 89 deletions(-) diff --git a/ghost/package-json/lib/filter.js b/ghost/package-json/lib/filter.js index c461599514..86843a2a9d 100644 --- a/ghost/package-json/lib/filter.js +++ b/ghost/package-json/lib/filter.js @@ -1,6 +1,6 @@ -var _ = require('lodash'), - notAPackageRegex = /^\.|_messages|README.md/i, - filterPackages; +const _ = require('lodash'); +const notAPackageRegex = /^\.|_messages|README.md/i; +let filterPackages; /** * ### Filter Packages @@ -26,7 +26,7 @@ filterPackages = function filterPackages(packages, active) { } return _.reduce(packages, function (result, pkg, key) { - var item = {}; + let item = {}; if (!key.match(notAPackageRegex)) { item = { name: key, diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js index 7b0454bf9e..a07d867869 100644 --- a/ghost/package-json/lib/parse.js +++ b/ghost/package-json/lib/parse.js @@ -2,9 +2,10 @@ * Dependencies */ -var Promise = require('bluebird'), - fs = require('fs-extra'), - common = require('../../common'); +const Promise = require('bluebird'); + +const fs = require('fs-extra'); +const common = require('../../common'); /** * Parse package.json and validate it has @@ -14,13 +15,15 @@ var Promise = require('bluebird'), function parsePackageJson(path) { return fs.readFile(path) .catch(function () { - var err = new Error(common.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); + const err = new Error(common.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); err.context = path; return Promise.reject(err); }) .then(function (source) { - var hasRequiredKeys, json, err; + let hasRequiredKeys; + let json; + let err; try { json = JSON.parse(source); diff --git a/ghost/package-json/lib/read.js b/ghost/package-json/lib/read.js index 6a10fab547..e6c94d29c6 100644 --- a/ghost/package-json/lib/read.js +++ b/ghost/package-json/lib/read.js @@ -1,25 +1,24 @@ /** * Dependencies */ -var Promise = require('bluebird'), - _ = require('lodash'), - join = require('path').join, - fs = require('fs-extra'), - parsePackageJson = require('./parse'), - common = require('../../common'), +const Promise = require('bluebird'); - notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i, - packageJSONPath = 'package.json', - - readPackage, - readPackages, - processPackage; +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) { - var pkg = { + const pkg = { name: packageName, path: absolutePath }; @@ -39,7 +38,7 @@ processPackage = function processPackage(absolutePath, packageName) { }; readPackage = function readPackage(packagePath, packageName) { - var absolutePath = join(packagePath, packageName); + const absolutePath = join(packagePath, packageName); return fs.stat(absolutePath) .then(function (stat) { if (!stat.isDirectory()) { @@ -48,7 +47,7 @@ readPackage = function readPackage(packagePath, packageName) { return processPackage(absolutePath, packageName) .then(function gotPackage(pkg) { - var res = {}; + const res = {}; res[packageName] = pkg; return res; }); @@ -76,7 +75,7 @@ readPackages = function readPackages(packagePath) { }); }) .map(function readPackageJson(packageName) { - var absolutePath = join(packagePath, packageName); + const absolutePath = join(packagePath, packageName); return processPackage(absolutePath, packageName); }) .then(function (packages) { diff --git a/ghost/package-json/test/filter_spec.js b/ghost/package-json/test/filter_spec.js index 3c7f3358d2..d3b1709a56 100644 --- a/ghost/package-json/test/filter_spec.js +++ b/ghost/package-json/test/filter_spec.js @@ -1,44 +1,46 @@ -var should = require('should'), - packageJSON = require('../../../../../core/server/lib/fs/package-json'); +const should = require('should'); +const packageJSON = require('../../../../../core/server/lib/fs/package-json'); describe('lib/fs/package-json', function () { // @TODO: introduce some non-theme package examples - var casper = { + const casper = { + name: 'casper', + path: '~/content/themes/casper', + 'package.json': { name: 'casper', - path: '~/content/themes/casper', - 'package.json': { - name: 'casper', - description: 'The default personal blogging theme for Ghost. Beautiful, minimal and responsive.', - demo: 'https://demo.ghost.io', - version: '1.3.5', - engines: {}, - license: 'MIT', - screenshots: {}, - author: {}, - gpm: {}, - keywords: {}, - repository: {}, - bugs: 'https://github.com/TryGhost/Casper/issues', - contributors: 'https://github.com/TryGhost/Casper/graphs/contributors' - } - }, - simplePackage = { + description: 'The default personal blogging theme for Ghost. Beautiful, minimal and responsive.', + demo: 'https://demo.ghost.io', + version: '1.3.5', + engines: {}, + license: 'MIT', + screenshots: {}, + author: {}, + gpm: {}, + keywords: {}, + repository: {}, + bugs: 'https://github.com/TryGhost/Casper/issues', + contributors: 'https://github.com/TryGhost/Casper/graphs/contributors' + } + }; + + const simplePackage = { + name: 'simple', + path: '~/content/themes/simple', + 'package.json': { name: 'simple', - path: '~/content/themes/simple', - 'package.json': { - name: 'simple', - version: '0.1.0' - } - }, - missingPackageJson = { - name: 'missing', - path: '~/content/themes/missing', - 'package.json': null - }; + version: '0.1.0' + } + }; + + const missingPackageJson = { + name: 'missing', + path: '~/content/themes/missing', + 'package.json': null + }; it('should filter packages correctly', function () { - var result = packageJSON.filter({casper: casper}), - package1; + const result = packageJSON.filter({casper: casper}); + let package1; result.should.be.an.Array().with.lengthOf(1); package1 = result[0]; @@ -51,8 +53,9 @@ describe('lib/fs/package-json', function () { }); it('should filter packages and handle a single active package string', function () { - var result = packageJSON.filter({casper: casper, simple: simplePackage}, 'casper'), - package1, package2; + const result = packageJSON.filter({casper: casper, simple: simplePackage}, 'casper'); + let package1; + let package2; result.should.be.an.Array().with.lengthOf(2); package1 = result[0]; @@ -72,8 +75,9 @@ describe('lib/fs/package-json', function () { }); it('should filter packages and handle an array of active packages', function () { - var result = packageJSON.filter({casper: casper, simple: simplePackage}, ['casper', 'simple']), - package1, package2; + const result = packageJSON.filter({casper: casper, simple: simplePackage}, ['casper', 'simple']); + let package1; + let package2; result.should.be.an.Array().with.lengthOf(2); package1 = result[0]; @@ -93,8 +97,9 @@ describe('lib/fs/package-json', function () { }); it('handles packages with no package.json even though this makes us sad', function () { - var result = packageJSON.filter({casper: casper, missing: missingPackageJson}, ['casper']), - package1, package2; + const result = packageJSON.filter({casper: casper, missing: missingPackageJson}, ['casper']); + let package1; + let package2; result.should.be.an.Array().with.lengthOf(2); package1 = result[0]; @@ -114,7 +119,7 @@ describe('lib/fs/package-json', function () { }); it('filters out things which are not packages', function () { - var result = packageJSON.filter({ + const result = packageJSON.filter({ '.git': {}, '.anything': {}, 'README.md': {}, _messages: {} }); result.should.be.an.Array().with.lengthOf(0); diff --git a/ghost/package-json/test/parse_spec.js b/ghost/package-json/test/parse_spec.js index 40212eef03..0de6ff8478 100644 --- a/ghost/package-json/test/parse_spec.js +++ b/ghost/package-json/test/parse_spec.js @@ -1,11 +1,12 @@ -var should = require('should'), - tmp = require('tmp'), - fs = require('fs-extra'), - packageJSON = require('../../../../../core/server/lib/fs/package-json'); +const should = require('should'); +const tmp = require('tmp'); +const fs = require('fs-extra'); +const packageJSON = require('../../../../../core/server/lib/fs/package-json'); describe('lib/fs/package-json: parse', function () { it('should parse valid package.json', function (done) { - var pkgJson, tmpFile; + let pkgJson; + let tmpFile; tmpFile = tmp.fileSync(); pkgJson = JSON.stringify({ @@ -29,7 +30,8 @@ describe('lib/fs/package-json: parse', function () { }); it('should fail when name is missing', function (done) { - var pkgJson, tmpFile; + let pkgJson; + let tmpFile; tmpFile = tmp.fileSync(); pkgJson = JSON.stringify({ @@ -54,7 +56,8 @@ describe('lib/fs/package-json: parse', function () { }); it('should fail when version is missing', function (done) { - var pkgJson, tmpFile; + let pkgJson; + let tmpFile; tmpFile = tmp.fileSync(); pkgJson = JSON.stringify({ @@ -79,7 +82,8 @@ describe('lib/fs/package-json: parse', function () { }); it('should fail when JSON is invalid', function (done) { - var pkgJson, tmpFile; + let pkgJson; + let tmpFile; tmpFile = tmp.fileSync(); pkgJson = '{name:"test"}'; @@ -102,7 +106,7 @@ describe('lib/fs/package-json: parse', function () { }); it('should fail when file is missing', function (done) { - var tmpFile = tmp.fileSync(); + const tmpFile = tmp.fileSync(); tmpFile.removeCallback(); packageJSON.parse(tmpFile.name) diff --git a/ghost/package-json/test/read_spec.js b/ghost/package-json/test/read_spec.js index 8dd25296d1..bebd09626b 100644 --- a/ghost/package-json/test/read_spec.js +++ b/ghost/package-json/test/read_spec.js @@ -1,13 +1,13 @@ -var should = require('should'), - tmp = require('tmp'), - join = require('path').join, - fs = require('fs-extra'), - packageJSON = require('../../../../../core/server/lib/fs/package-json'); +const should = require('should'); +const tmp = require('tmp'); +const join = require('path').join; +const fs = require('fs-extra'); +const packageJSON = require('../../../../../core/server/lib/fs/package-json'); describe('lib/fs/package-json: read', function () { describe('all', function () { it('should read directory and ignore unneeded items', function (done) { - var packagePath = tmp.dirSync({unsafeCleanup: true}); + const packagePath = tmp.dirSync({unsafeCleanup: true}); // create example theme fs.mkdirSync(join(packagePath.name, 'casper')); @@ -36,7 +36,8 @@ describe('lib/fs/package-json: read', function () { }); it('should read directory and parse package.json files', function (done) { - var packagePath, pkgJson; + let packagePath; + let pkgJson; packagePath = tmp.dirSync({unsafeCleanup: true}); pkgJson = JSON.stringify({ @@ -69,7 +70,8 @@ describe('lib/fs/package-json: read', function () { }); it('should read directory and ignore invalid package.json files', function (done) { - var packagePath, pkgJson; + let packagePath; + let pkgJson; packagePath = tmp.dirSync({unsafeCleanup: true}); pkgJson = JSON.stringify({ @@ -100,7 +102,7 @@ describe('lib/fs/package-json: read', function () { describe('one', function () { it('should read directory and ignore unneeded items', function (done) { - var packagePath = tmp.dirSync({unsafeCleanup: true}); + const packagePath = tmp.dirSync({unsafeCleanup: true}); // create example theme fs.mkdirSync(join(packagePath.name, 'casper')); @@ -129,7 +131,8 @@ describe('lib/fs/package-json: read', function () { }); it('should read directory and parse package.json files', function (done) { - var packagePath, pkgJson; + let packagePath; + let pkgJson; packagePath = tmp.dirSync({unsafeCleanup: true}); pkgJson = JSON.stringify({ @@ -162,7 +165,8 @@ describe('lib/fs/package-json: read', function () { }); it('should read directory and ignore invalid package.json files', function (done) { - var packagePath, pkgJson; + let packagePath; + let pkgJson; packagePath = tmp.dirSync({unsafeCleanup: true}); pkgJson = JSON.stringify({ @@ -191,7 +195,7 @@ describe('lib/fs/package-json: read', function () { }); it('should read directory and include only single requested package', function (done) { - var packagePath = tmp.dirSync({unsafeCleanup: true}); + const packagePath = tmp.dirSync({unsafeCleanup: true}); // create trash fs.writeFileSync(join(packagePath.name, 'casper.zip')); @@ -222,7 +226,7 @@ describe('lib/fs/package-json: read', function () { }); it('should return an error if package cannot be found', function (done) { - var packagePath = tmp.dirSync({unsafeCleanup: true}); + const packagePath = tmp.dirSync({unsafeCleanup: true}); // create trash fs.writeFileSync(join(packagePath.name, 'casper.zip')); @@ -240,7 +244,7 @@ describe('lib/fs/package-json: read', function () { }); it('should return empty object if package is not a directory', function (done) { - var packagePath = tmp.dirSync({unsafeCleanup: true}); + const packagePath = tmp.dirSync({unsafeCleanup: true}); // create trash fs.writeFileSync(join(packagePath.name, 'casper.zip')); From 18ce837e9ac4a1001b309cbd3bb6c8b52befca84 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 30 Apr 2020 20:26:12 +0100 Subject: [PATCH 13/27] Refactor common pattern in service files - Use array destructuring - Use @tryghost/errors - Part of the big move towards decoupling, this gives visibility on what's being used where - Biting off manageable chunks / fixing bits of code I'm refactoring for other reasons --- ghost/package-json/lib/parse.js | 12 ++++++------ ghost/package-json/lib/read.js | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js index a07d867869..023615373f 100644 --- a/ghost/package-json/lib/parse.js +++ b/ghost/package-json/lib/parse.js @@ -5,7 +5,7 @@ const Promise = require('bluebird'); const fs = require('fs-extra'); -const common = require('../../common'); +const {i18n} = require('../../common'); /** * Parse package.json and validate it has @@ -15,7 +15,7 @@ const common = require('../../common'); function parsePackageJson(path) { return fs.readFile(path) .catch(function () { - const err = new Error(common.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); + const err = new Error(i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); err.context = path; return Promise.reject(err); @@ -31,18 +31,18 @@ function parsePackageJson(path) { hasRequiredKeys = json.name && json.version; if (!hasRequiredKeys) { - err = new Error(common.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); + err = new Error(i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); + err.help = i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); return Promise.reject(err); } return json; } catch (parseError) { - err = new Error(common.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); + err = new Error(i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); err.context = path; - err.help = common.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); + err.help = i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); return Promise.reject(err); } diff --git a/ghost/package-json/lib/read.js b/ghost/package-json/lib/read.js index e6c94d29c6..732a1af688 100644 --- a/ghost/package-json/lib/read.js +++ b/ghost/package-json/lib/read.js @@ -7,7 +7,7 @@ const _ = require('lodash'); const join = require('path').join; const fs = require('fs-extra'); const parsePackageJson = require('./parse'); -const common = require('../../common'); +const errors = require('@tryghost/errors'); const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i; const packageJSONPath = 'package.json'; let readPackage; @@ -53,7 +53,7 @@ readPackage = function readPackage(packagePath, packageName) { }); }) .catch(function (err) { - return Promise.reject(new common.errors.NotFoundError({ + return Promise.reject(new errors.NotFoundError({ message: 'Package not found', err: err, help: 'path: ' + packagePath, From c4b6351a74358e583955f0d8f38b702ab38b2006 Mon Sep 17 00:00:00 2001 From: naz Date: Tue, 20 Oct 2020 12:02:56 +1300 Subject: [PATCH 14/27] Fixed "no-shadow" linting error in server modules (#12287) refs 143921948de89baff21a800ae8e7dfaeef415b05 - Continuation of changes started in referenced commit --- ghost/package-json/lib/filter.js | 3 +-- ghost/package-json/lib/read.js | 9 +++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/ghost/package-json/lib/filter.js b/ghost/package-json/lib/filter.js index 86843a2a9d..7e83e22746 100644 --- a/ghost/package-json/lib/filter.js +++ b/ghost/package-json/lib/filter.js @@ -1,6 +1,5 @@ const _ = require('lodash'); const notAPackageRegex = /^\.|_messages|README.md/i; -let filterPackages; /** * ### Filter Packages @@ -19,7 +18,7 @@ let filterPackages; * @param {array/string} active as read from the settings object * @returns {Array} of objects with useful info about themes */ -filterPackages = function filterPackages(packages, active) { +const filterPackages = function filterPackages(packages, active) { // turn active into an array if it isn't one, so this function can deal with lists and one-offs if (!Array.isArray(active)) { active = [active]; diff --git a/ghost/package-json/lib/read.js b/ghost/package-json/lib/read.js index 732a1af688..0753d8d110 100644 --- a/ghost/package-json/lib/read.js +++ b/ghost/package-json/lib/read.js @@ -10,14 +10,11 @@ const parsePackageJson = require('./parse'); const errors = require('@tryghost/errors'); 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 processPackage = function processPackage(absolutePath, packageName) { const pkg = { name: packageName, path: absolutePath @@ -37,7 +34,7 @@ processPackage = function processPackage(absolutePath, packageName) { }); }; -readPackage = function readPackage(packagePath, packageName) { +const readPackage = function readPackage(packagePath, packageName) { const absolutePath = join(packagePath, packageName); return fs.stat(absolutePath) .then(function (stat) { @@ -62,7 +59,7 @@ readPackage = function readPackage(packagePath, packageName) { }); }; -readPackages = function readPackages(packagePath) { +const readPackages = function readPackages(packagePath) { return fs.readdir(packagePath) .filter(function (packageName) { // Filter out things which are not packages by regex From e37d08b5d0c3e7a19d2afa428f8b68cacc93cc87 Mon Sep 17 00:00:00 2001 From: Austin Burdine Date: Fri, 9 Oct 2020 09:52:25 -0400 Subject: [PATCH 15/27] =?UTF-8?q?=F0=9F=94=A5=20Added=20support=20for=20No?= =?UTF-8?q?de=2014?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ghost/package-json/test/read_spec.js | 34 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/ghost/package-json/test/read_spec.js b/ghost/package-json/test/read_spec.js index bebd09626b..48322e77cf 100644 --- a/ghost/package-json/test/read_spec.js +++ b/ghost/package-json/test/read_spec.js @@ -11,13 +11,13 @@ describe('lib/fs/package-json: read', function () { // create example theme fs.mkdirSync(join(packagePath.name, 'casper')); - fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs')); + fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs'), ''); // create some trash fs.mkdirSync(join(packagePath.name, 'node_modules')); fs.mkdirSync(join(packagePath.name, 'bower_components')); fs.mkdirSync(join(packagePath.name, '.git')); - fs.writeFileSync(join(packagePath.name, '.DS_Store')); + fs.writeFileSync(join(packagePath.name, '.DS_Store'), ''); packageJSON.read.all(packagePath.name) .then(function (pkgs) { @@ -48,7 +48,7 @@ describe('lib/fs/package-json: read', function () { // create example theme fs.mkdirSync(join(packagePath.name, 'testtheme')); fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); - fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs')); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), ''); packageJSON.read.all(packagePath.name) .then(function (pkgs) { @@ -81,7 +81,7 @@ describe('lib/fs/package-json: read', function () { // create example theme fs.mkdirSync(join(packagePath.name, 'testtheme')); fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); - fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs')); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), ''); packageJSON.read.all(packagePath.name) .then(function (pkgs) { @@ -106,13 +106,13 @@ describe('lib/fs/package-json: read', function () { // create example theme fs.mkdirSync(join(packagePath.name, 'casper')); - fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs')); + fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs'), ''); // create some trash fs.mkdirSync(join(packagePath.name, 'node_modules')); fs.mkdirSync(join(packagePath.name, 'bower_components')); fs.mkdirSync(join(packagePath.name, '.git')); - fs.writeFileSync(join(packagePath.name, '.DS_Store')); + fs.writeFileSync(join(packagePath.name, '.DS_Store'), ''); packageJSON.read.one(packagePath.name, 'casper') .then(function (pkgs) { @@ -143,7 +143,7 @@ describe('lib/fs/package-json: read', function () { // create example theme fs.mkdirSync(join(packagePath.name, 'testtheme')); fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); - fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs')); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), ''); packageJSON.read.one(packagePath.name, 'testtheme') .then(function (pkgs) { @@ -176,7 +176,7 @@ describe('lib/fs/package-json: read', function () { // create example theme fs.mkdirSync(join(packagePath.name, 'testtheme')); fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); - fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs')); + fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), ''); packageJSON.read.one(packagePath.name, 'testtheme') .then(function (pkgs) { @@ -198,16 +198,16 @@ describe('lib/fs/package-json: read', function () { const packagePath = tmp.dirSync({unsafeCleanup: true}); // create trash - fs.writeFileSync(join(packagePath.name, 'casper.zip')); - fs.writeFileSync(join(packagePath.name, '.DS_Store')); + fs.writeFileSync(join(packagePath.name, 'casper.zip'), ''); + fs.writeFileSync(join(packagePath.name, '.DS_Store'), ''); // create actual theme fs.mkdirSync(join(packagePath.name, 'casper')); fs.mkdirSync(join(packagePath.name, 'casper', 'partials')); - fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs')); - fs.writeFileSync(join(packagePath.name, 'casper', 'partials', 'navigation.hbs')); + fs.writeFileSync(join(packagePath.name, 'casper', 'index.hbs'), ''); + fs.writeFileSync(join(packagePath.name, 'casper', 'partials', 'navigation.hbs'), ''); fs.mkdirSync(join(packagePath.name, 'not-casper')); - fs.writeFileSync(join(packagePath.name, 'not-casper', 'index.hbs')); + fs.writeFileSync(join(packagePath.name, 'not-casper', 'index.hbs'), ''); packageJSON.read.one(packagePath.name, 'casper') .then(function (pkgs) { @@ -229,8 +229,8 @@ describe('lib/fs/package-json: read', function () { const packagePath = tmp.dirSync({unsafeCleanup: true}); // create trash - fs.writeFileSync(join(packagePath.name, 'casper.zip')); - fs.writeFileSync(join(packagePath.name, '.DS_Store')); + fs.writeFileSync(join(packagePath.name, 'casper.zip'), ''); + fs.writeFileSync(join(packagePath.name, '.DS_Store'), ''); packageJSON.read.one(packagePath.name, 'casper') .then(function () { @@ -247,8 +247,8 @@ describe('lib/fs/package-json: read', function () { const packagePath = tmp.dirSync({unsafeCleanup: true}); // create trash - fs.writeFileSync(join(packagePath.name, 'casper.zip')); - fs.writeFileSync(join(packagePath.name, '.DS_Store')); + fs.writeFileSync(join(packagePath.name, 'casper.zip'), ''); + fs.writeFileSync(join(packagePath.name, '.DS_Store'), ''); packageJSON.read.one(packagePath.name, 'casper.zip') .then(function (pkg) { From 1d707b59a60114a73b9b88357b806199fd14bbef Mon Sep 17 00:00:00 2001 From: Kukhyeon Heo Date: Wed, 4 Nov 2020 19:55:47 +0900 Subject: [PATCH 16/27] Removed global.Promise override (#12182) closed #11943 * Remove global.Promise * Fix brute-knex bluebird error. * Fix api-acceptance tests. * Fix unit tests --- ghost/package-json/lib/read.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ghost/package-json/lib/read.js b/ghost/package-json/lib/read.js index 0753d8d110..c019e0c21e 100644 --- a/ghost/package-json/lib/read.js +++ b/ghost/package-json/lib/read.js @@ -60,7 +60,7 @@ const readPackage = function readPackage(packagePath, packageName) { }; const readPackages = function readPackages(packagePath) { - return fs.readdir(packagePath) + return Promise.resolve(fs.readdir(packagePath)) .filter(function (packageName) { // Filter out things which are not packages by regex if (packageName.match(notAPackageRegex)) { From 9d29686f6a734b8aa66d4c45b7bd52baf33a7210 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Mon, 23 Nov 2020 21:48:32 +0000 Subject: [PATCH 17/27] Refactored package-json lib into a class - this helps bring all the code together so we can extract it in the future - turning it into a class also lets us easily inject the i18n instance and store it locally --- ghost/package-json/lib/filter.js | 43 ------ ghost/package-json/lib/index.js | 25 +--- ghost/package-json/lib/package-json.js | 179 +++++++++++++++++++++++++ ghost/package-json/lib/parse.js | 56 -------- ghost/package-json/lib/read.js | 87 ------------ ghost/package-json/test/read_spec.js | 18 +-- 6 files changed, 191 insertions(+), 217 deletions(-) delete mode 100644 ghost/package-json/lib/filter.js create mode 100644 ghost/package-json/lib/package-json.js delete mode 100644 ghost/package-json/lib/parse.js delete mode 100644 ghost/package-json/lib/read.js diff --git a/ghost/package-json/lib/filter.js b/ghost/package-json/lib/filter.js deleted file mode 100644 index 7e83e22746..0000000000 --- a/ghost/package-json/lib/filter.js +++ /dev/null @@ -1,43 +0,0 @@ -const _ = require('lodash'); -const notAPackageRegex = /^\.|_messages|README.md/i; - -/** - * ### Filter Packages - * Normalizes packages read by read-packages so that the themes module can use them. - * Iterates over each package and return an array of objects which are simplified representations of the package - * with 3 properties: - * - `name` - the package name - * - `package` - contents of the package.json or false if there isn't one - * - `active` - set to true if this package is active - * This data structure is used for listings of packages provided over the API and as such - * deliberately combines multiple sources of information in order to be efficient. - * - * TODO: simplify the package.json representation to contain only fields we use - * - * @param {object} packages as returned by read-packages - * @param {array/string} active as read from the settings object - * @returns {Array} of objects with useful info about themes - */ -const filterPackages = function filterPackages(packages, active) { - // turn active into an array if it isn't one, so this function can deal with lists and one-offs - if (!Array.isArray(active)) { - active = [active]; - } - - return _.reduce(packages, function (result, pkg, key) { - let item = {}; - if (!key.match(notAPackageRegex)) { - item = { - name: key, - package: pkg['package.json'] || false, - active: _.indexOf(active, key) !== -1 - }; - - result.push(item); - } - - return result; - }, []); -}; - -module.exports = filterPackages; diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js index cbc3cb022f..3f50a1a568 100644 --- a/ghost/package-json/lib/index.js +++ b/ghost/package-json/lib/index.js @@ -1,23 +1,4 @@ -/** - * # Package Utils - * - * Ghost has / is in the process of gaining support for several different types of sub-packages: - * - Themes: have always been packages, but we're going to lean more heavily on npm & package.json in future - * - Adapters: replace fundamental pieces like storage, will become npm modules - * - * These utils facilitate loading, reading, managing etc, packages from the file system. - */ +const PackageJson = require('./package-json'); +const {i18n} = require('../../common'); -module.exports = { - get read() { - return require('./read'); - }, - - get parse() { - return require('./parse'); - }, - - get filter() { - return require('./filter'); - } -}; +module.exports = new PackageJson(i18n); diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js new file mode 100644 index 0000000000..416b067c16 --- /dev/null +++ b/ghost/package-json/lib/package-json.js @@ -0,0 +1,179 @@ +const _ = require('lodash'); +const Promise = require('bluebird'); +const fs = require('fs-extra'); +const join = require('path').join; +const errors = require('@tryghost/errors'); + +const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i; +const packageJSONPath = 'package.json'; + +/** + * # Package Utils + * + * Ghost has / is in the process of gaining support for several different types of sub-packages: + * - Themes: have always been packages, but we're going to lean more heavily on npm & package.json in future + * - Adapters: replace fundamental pieces like storage, will become npm modules + * + * These utils facilitate loading, reading, managing etc, packages from the file system. + */ +module.exports = class PackageJson { + constructor(i18n) { + this.i18n = i18n; + } + + /** + * ### Filter Packages + * Normalizes packages read by read-packages so that the themes module can use them. + * Iterates over each package and return an array of objects which are simplified representations of the package + * with 3 properties: + * - `name` - the package name + * - `package` - contents of the package.json or false if there isn't one + * - `active` - set to true if this package is active + * This data structure is used for listings of packages provided over the API and as such + * deliberately combines multiple sources of information in order to be efficient. + * + * TODO: simplify the package.json representation to contain only fields we use + * + * @param {object} packages as returned by read-packages + * @param {array/string} active as read from the settings object + * @returns {Array} of objects with useful info about themes + */ + filter(packages, active) { + // turn active into an array if it isn't one, so this function can deal with lists and one-offs + if (!Array.isArray(active)) { + active = [active]; + } + + return _.reduce(packages, function (result, pkg, key) { + let item = {}; + if (!key.match(notAPackageRegex)) { + item = { + name: key, + package: pkg['package.json'] || false, + active: _.indexOf(active, key) !== -1 + }; + + result.push(item); + } + + return result; + }, []); + } + + /** + * Parse package.json and validate it has + * all the required fields + */ + parse(path) { + const self = this; + + return fs.readFile(path) + .catch(function () { + const err = new Error(self.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); + err.context = path; + + return Promise.reject(err); + }) + .then(function (source) { + let hasRequiredKeys; + let json; + let err; + + try { + json = JSON.parse(source); + + hasRequiredKeys = json.name && json.version; + + if (!hasRequiredKeys) { + err = new Error(self.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); + err.context = path; + err.help = self.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); + + return Promise.reject(err); + } + + return json; + } catch (parseError) { + err = new Error(self.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); + err.context = path; + err.help = self.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); + + return Promise.reject(err); + } + }); + } + + /** + * Recursively read directory and find the packages in it + * + * @returns {object} + */ + processPackage(absolutePath, packageName) { + const pkg = { + name: packageName, + path: absolutePath + }; + return this.parse(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(packagePath, packageName) { + const self = this; + const absolutePath = join(packagePath, packageName); + return fs.stat(absolutePath) + .then(function (stat) { + if (!stat.isDirectory()) { + return {}; + } + + return self.processPackage(absolutePath, packageName) + .then(function gotPackage(pkg) { + const res = {}; + res[packageName] = pkg; + return res; + }); + }) + .catch(function (err) { + return Promise.reject(new errors.NotFoundError({ + message: 'Package not found', + err: err, + help: 'path: ' + packagePath, + context: 'name: ' + packageName + })); + }); + } + + readPackages(packagePath) { + const self = this; + + return Promise.resolve(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 self.processPackage(absolutePath, packageName); + }) + .then(function (packages) { + return _.keyBy(packages, 'name'); + }); + } +}; diff --git a/ghost/package-json/lib/parse.js b/ghost/package-json/lib/parse.js deleted file mode 100644 index 023615373f..0000000000 --- a/ghost/package-json/lib/parse.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Dependencies - */ - -const Promise = require('bluebird'); - -const fs = require('fs-extra'); -const {i18n} = require('../../common'); - -/** - * Parse package.json and validate it has - * all the required fields - */ - -function parsePackageJson(path) { - return fs.readFile(path) - .catch(function () { - const err = new Error(i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); - err.context = path; - - return Promise.reject(err); - }) - .then(function (source) { - let hasRequiredKeys; - let json; - let err; - - try { - json = JSON.parse(source); - - hasRequiredKeys = json.name && json.version; - - if (!hasRequiredKeys) { - err = new Error(i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); - err.context = path; - err.help = i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); - - return Promise.reject(err); - } - - return json; - } catch (parseError) { - err = new Error(i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); - err.context = path; - err.help = i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); - - return Promise.reject(err); - } - }); -} - -/** - * Expose `parsePackageJson` - */ - -module.exports = parsePackageJson; diff --git a/ghost/package-json/lib/read.js b/ghost/package-json/lib/read.js deleted file mode 100644 index c019e0c21e..0000000000 --- a/ghost/package-json/lib/read.js +++ /dev/null @@ -1,87 +0,0 @@ -/** - * Dependencies - */ -const Promise = require('bluebird'); - -const _ = require('lodash'); -const join = require('path').join; -const fs = require('fs-extra'); -const parsePackageJson = require('./parse'); -const errors = require('@tryghost/errors'); -const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i; -const packageJSONPath = 'package.json'; - -/** - * Recursively read directory and find the packages in it - */ -const 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; - }); -}; - -const 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 errors.NotFoundError({ - message: 'Package not found', - err: err, - help: 'path: ' + packagePath, - context: 'name: ' + packageName - })); - }); -}; - -const readPackages = function readPackages(packagePath) { - return Promise.resolve(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; diff --git a/ghost/package-json/test/read_spec.js b/ghost/package-json/test/read_spec.js index 48322e77cf..7fb29c8aee 100644 --- a/ghost/package-json/test/read_spec.js +++ b/ghost/package-json/test/read_spec.js @@ -19,7 +19,7 @@ describe('lib/fs/package-json: read', function () { fs.mkdirSync(join(packagePath.name, '.git')); fs.writeFileSync(join(packagePath.name, '.DS_Store'), ''); - packageJSON.read.all(packagePath.name) + packageJSON.readPackages(packagePath.name) .then(function (pkgs) { pkgs.should.eql({ casper: { @@ -50,7 +50,7 @@ describe('lib/fs/package-json: read', function () { fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), ''); - packageJSON.read.all(packagePath.name) + packageJSON.readPackages(packagePath.name) .then(function (pkgs) { pkgs.should.eql({ testtheme: { @@ -83,7 +83,7 @@ describe('lib/fs/package-json: read', function () { fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), ''); - packageJSON.read.all(packagePath.name) + packageJSON.readPackages(packagePath.name) .then(function (pkgs) { pkgs.should.eql({ testtheme: { @@ -114,7 +114,7 @@ describe('lib/fs/package-json: read', function () { fs.mkdirSync(join(packagePath.name, '.git')); fs.writeFileSync(join(packagePath.name, '.DS_Store'), ''); - packageJSON.read.one(packagePath.name, 'casper') + packageJSON.readPackage(packagePath.name, 'casper') .then(function (pkgs) { pkgs.should.eql({ casper: { @@ -145,7 +145,7 @@ describe('lib/fs/package-json: read', function () { fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), ''); - packageJSON.read.one(packagePath.name, 'testtheme') + packageJSON.readPackage(packagePath.name, 'testtheme') .then(function (pkgs) { pkgs.should.eql({ testtheme: { @@ -178,7 +178,7 @@ describe('lib/fs/package-json: read', function () { fs.writeFileSync(join(packagePath.name, 'testtheme', 'package.json'), pkgJson); fs.writeFileSync(join(packagePath.name, 'testtheme', 'index.hbs'), ''); - packageJSON.read.one(packagePath.name, 'testtheme') + packageJSON.readPackage(packagePath.name, 'testtheme') .then(function (pkgs) { pkgs.should.eql({ testtheme: { @@ -209,7 +209,7 @@ describe('lib/fs/package-json: read', function () { fs.mkdirSync(join(packagePath.name, 'not-casper')); fs.writeFileSync(join(packagePath.name, 'not-casper', 'index.hbs'), ''); - packageJSON.read.one(packagePath.name, 'casper') + packageJSON.readPackage(packagePath.name, 'casper') .then(function (pkgs) { pkgs.should.eql({ casper: { @@ -232,7 +232,7 @@ describe('lib/fs/package-json: read', function () { fs.writeFileSync(join(packagePath.name, 'casper.zip'), ''); fs.writeFileSync(join(packagePath.name, '.DS_Store'), ''); - packageJSON.read.one(packagePath.name, 'casper') + packageJSON.readPackage(packagePath.name, 'casper') .then(function () { done('Should have thrown an error'); }) @@ -250,7 +250,7 @@ describe('lib/fs/package-json: read', function () { fs.writeFileSync(join(packagePath.name, 'casper.zip'), ''); fs.writeFileSync(join(packagePath.name, '.DS_Store'), ''); - packageJSON.read.one(packagePath.name, 'casper.zip') + packageJSON.readPackage(packagePath.name, 'casper.zip') .then(function (pkg) { pkg.should.eql({}); From 0971506948bb1d8e624bbf8029427f9738111114 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Tue, 24 Nov 2020 15:57:56 +0000 Subject: [PATCH 18/27] Refactored package-json lib to use more async-await - this helps get rid of all the promise chaining and indentation, resulting in cleaner code --- ghost/package-json/lib/package-json.js | 126 ++++++++++++------------- 1 file changed, 61 insertions(+), 65 deletions(-) diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js index 416b067c16..0b3045fd77 100644 --- a/ghost/package-json/lib/package-json.js +++ b/ghost/package-json/lib/package-json.js @@ -64,43 +64,42 @@ module.exports = class PackageJson { * Parse package.json and validate it has * all the required fields */ - parse(path) { - const self = this; + async parse(path) { + let source; + let json; - return fs.readFile(path) - .catch(function () { - const err = new Error(self.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); - err.context = path; + try { + source = await fs.readFile(path); + } catch (readError) { + const err = new Error(this.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); + err.context = path; + err.err = readError; - return Promise.reject(err); - }) - .then(function (source) { - let hasRequiredKeys; - let json; - let err; + return Promise.reject(err); + } - try { - json = JSON.parse(source); + try { + json = JSON.parse(source); + } catch (parseError) { + const err = new Error(this.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); + err.context = path; + err.err = parseError; + err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); - hasRequiredKeys = json.name && json.version; + return Promise.reject(err); + } - if (!hasRequiredKeys) { - err = new Error(self.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); - err.context = path; - err.help = self.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); + const hasRequiredKeys = json.name && json.version; - return Promise.reject(err); - } + if (!hasRequiredKeys) { + const err = new Error(this.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); + err.context = path; + err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); - return json; - } catch (parseError) { - err = new Error(self.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); - err.context = path; - err.help = self.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); + return Promise.reject(err); + } - return Promise.reject(err); - } - }); + return json; } /** @@ -108,50 +107,47 @@ module.exports = class PackageJson { * * @returns {object} */ - processPackage(absolutePath, packageName) { + async processPackage(absolutePath, packageName) { const pkg = { name: packageName, path: absolutePath }; - return this.parse(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; - }); + + try { + const packageJSON = await this.parse(join(absolutePath, packageJSONPath)); + pkg['package.json'] = packageJSON; + } catch (err) { + // 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(packagePath, packageName) { - const self = this; + async readPackage(packagePath, packageName) { const absolutePath = join(packagePath, packageName); - return fs.stat(absolutePath) - .then(function (stat) { - if (!stat.isDirectory()) { - return {}; - } - return self.processPackage(absolutePath, packageName) - .then(function gotPackage(pkg) { - const res = {}; - res[packageName] = pkg; - return res; - }); - }) - .catch(function (err) { - return Promise.reject(new errors.NotFoundError({ - message: 'Package not found', - err: err, - help: 'path: ' + packagePath, - context: 'name: ' + packageName - })); - }); + try { + const stat = await fs.stat(absolutePath); + if (!stat.isDirectory()) { + return {}; + } + + const pkg = await this.processPackage(absolutePath, packageName); + const res = {}; + res[packageName] = pkg; + return res; + } catch (err) { + return Promise.reject(new errors.NotFoundError({ + message: 'Package not found', + err: err, + help: 'path: ' + packagePath, + context: 'name: ' + packageName + })); + } } readPackages(packagePath) { From 2be01e7cbd7c7670256ad58947dbeae79bb0fd2c Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Wed, 25 Nov 2020 10:44:15 +0000 Subject: [PATCH 19/27] Refactored remaining function in package-json lib to use async-await - this helps simplify the code and gets rid of Promise chaining - apparently I can't easily use an async function within filter, so I've left it for now --- ghost/package-json/lib/package-json.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js index 0b3045fd77..e417e43907 100644 --- a/ghost/package-json/lib/package-json.js +++ b/ghost/package-json/lib/package-json.js @@ -150,10 +150,10 @@ module.exports = class PackageJson { } } - readPackages(packagePath) { - const self = this; + async readPackages(packagePath) { + const dirContents = await fs.readdir(packagePath); - return Promise.resolve(fs.readdir(packagePath)) + const packageNames = dirContents .filter(function (packageName) { // Filter out things which are not packages by regex if (packageName.match(notAPackageRegex)) { @@ -163,13 +163,14 @@ module.exports = class PackageJson { return fs.stat(join(packagePath, packageName)).then(function (stat) { return stat.isDirectory(); }); - }) - .map(function readPackageJson(packageName) { - const absolutePath = join(packagePath, packageName); - return self.processPackage(absolutePath, packageName); - }) - .then(function (packages) { - return _.keyBy(packages, 'name'); }); + + const packages = await Promise.all(packageNames + .map((packageName) => { + const absolutePath = join(packagePath, packageName); + return this.processPackage(absolutePath, packageName); + })); + + return _.keyBy(packages, 'name'); } }; From 7f09052ba18499ea41f5763f21bd31ba69a05369 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Wed, 25 Nov 2020 10:57:55 +0000 Subject: [PATCH 20/27] Reverted "Refactored remaining function in package-json lib to use async-await" - this reverts commit 2be01e7cbd7c7670256ad58947dbeae79bb0fd2c. - reverting until I can figure out why the tests are broken --- ghost/package-json/lib/package-json.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js index e417e43907..0b3045fd77 100644 --- a/ghost/package-json/lib/package-json.js +++ b/ghost/package-json/lib/package-json.js @@ -150,10 +150,10 @@ module.exports = class PackageJson { } } - async readPackages(packagePath) { - const dirContents = await fs.readdir(packagePath); + readPackages(packagePath) { + const self = this; - const packageNames = dirContents + return Promise.resolve(fs.readdir(packagePath)) .filter(function (packageName) { // Filter out things which are not packages by regex if (packageName.match(notAPackageRegex)) { @@ -163,14 +163,13 @@ module.exports = class PackageJson { return fs.stat(join(packagePath, packageName)).then(function (stat) { return stat.isDirectory(); }); - }); - - const packages = await Promise.all(packageNames - .map((packageName) => { + }) + .map(function readPackageJson(packageName) { const absolutePath = join(packagePath, packageName); - return this.processPackage(absolutePath, packageName); - })); - - return _.keyBy(packages, 'name'); + return self.processPackage(absolutePath, packageName); + }) + .then(function (packages) { + return _.keyBy(packages, 'name'); + }); } }; From 6d42c391c53064eeca5311230930b2e545cb13d6 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Wed, 25 Nov 2020 11:29:04 +0000 Subject: [PATCH 21/27] Added JSDoc comments into package-json lib --- ghost/package-json/lib/package-json.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js index 0b3045fd77..0e9628be01 100644 --- a/ghost/package-json/lib/package-json.js +++ b/ghost/package-json/lib/package-json.js @@ -63,6 +63,8 @@ module.exports = class PackageJson { /** * Parse package.json and validate it has * all the required fields + * + * @param {string} path */ async parse(path) { let source; @@ -105,6 +107,8 @@ module.exports = class PackageJson { /** * Recursively read directory and find the packages in it * + * @param {string} absolutePath + * @param {string} packageName * @returns {object} */ async processPackage(absolutePath, packageName) { @@ -127,6 +131,10 @@ module.exports = class PackageJson { return pkg; } + /** + * @param {string} packagePath + * @param {string} packageName + */ async readPackage(packagePath, packageName) { const absolutePath = join(packagePath, packageName); @@ -150,6 +158,9 @@ module.exports = class PackageJson { } } + /** + * @param {string} packagePath + */ readPackages(packagePath) { const self = this; From bd3afeb112fc2bab896c42e9186c69e6bc907e29 Mon Sep 17 00:00:00 2001 From: Thibaut Patel Date: Thu, 10 Dec 2020 11:37:43 +0100 Subject: [PATCH 22/27] Made the package-json module ready to be exported (#12451) no issue --- ghost/package-json/lib/index.js | 2 +- ghost/package-json/lib/package-json.js | 13 ++++++++++++- ghost/package-json/test/filter_spec.js | 8 +++++++- ghost/package-json/test/parse_spec.js | 22 ++++++++++++++-------- ghost/package-json/test/read_spec.js | 8 +++++++- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js index 3f50a1a568..89b8232e17 100644 --- a/ghost/package-json/lib/index.js +++ b/ghost/package-json/lib/index.js @@ -1,4 +1,4 @@ const PackageJson = require('./package-json'); const {i18n} = require('../../common'); -module.exports = new PackageJson(i18n); +module.exports = new PackageJson({i18n}); diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js index 0e9628be01..e5b8d46e38 100644 --- a/ghost/package-json/lib/package-json.js +++ b/ghost/package-json/lib/package-json.js @@ -7,6 +7,11 @@ const errors = require('@tryghost/errors'); const notAPackageRegex = /^\.|_messages|README.md|node_modules|bower_components/i; const packageJSONPath = 'package.json'; +/** + * @typedef {Object} Ii18n + * @prop {(key: string) => string} t + */ + /** * # Package Utils * @@ -15,9 +20,15 @@ const packageJSONPath = 'package.json'; * - Adapters: replace fundamental pieces like storage, will become npm modules * * These utils facilitate loading, reading, managing etc, packages from the file system. + * */ module.exports = class PackageJson { - constructor(i18n) { + /** + * Creates an instance of PackageJson, an util used to read and validate package.json files + * @param {Object} dependencies + * @param {Ii18n} dependencies.i18n + */ + constructor({i18n}) { this.i18n = i18n; } diff --git a/ghost/package-json/test/filter_spec.js b/ghost/package-json/test/filter_spec.js index d3b1709a56..854f810708 100644 --- a/ghost/package-json/test/filter_spec.js +++ b/ghost/package-json/test/filter_spec.js @@ -1,5 +1,11 @@ const should = require('should'); -const packageJSON = require('../../../../../core/server/lib/fs/package-json'); +const PackageJSON = require('../../../../../core/server/lib/fs/package-json/package-json'); + +const packageJSON = new PackageJSON({ + i18n: { + t: key => key + } +}); describe('lib/fs/package-json', function () { // @TODO: introduce some non-theme package examples diff --git a/ghost/package-json/test/parse_spec.js b/ghost/package-json/test/parse_spec.js index 0de6ff8478..ffe3b206ac 100644 --- a/ghost/package-json/test/parse_spec.js +++ b/ghost/package-json/test/parse_spec.js @@ -1,7 +1,13 @@ const should = require('should'); const tmp = require('tmp'); const fs = require('fs-extra'); -const packageJSON = require('../../../../../core/server/lib/fs/package-json'); +const PackageJSON = require('../../../../../core/server/lib/fs/package-json/package-json'); + +const packageJSON = new PackageJSON({ + i18n: { + t: key => key + } +}); describe('lib/fs/package-json: parse', function () { it('should parse valid package.json', function (done) { @@ -45,9 +51,9 @@ describe('lib/fs/package-json: parse', function () { done(new Error('packageJSON.parse succeeded, but should\'ve failed')); }) .catch(function (err) { - err.message.should.equal('"name" or "version" is missing from theme package.json file.'); + err.message.should.equal('errors.utils.parsepackagejson.nameOrVersionMissing'); err.context.should.equal(tmpFile.name); - err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/'); + err.help.should.equal('errors.utils.parsepackagejson.willBeRequired'); done(); }) @@ -71,9 +77,9 @@ describe('lib/fs/package-json: parse', function () { done(new Error('packageJSON.parse succeeded, but should\'ve failed')); }) .catch(function (err) { - err.message.should.equal('"name" or "version" is missing from theme package.json file.'); + err.message.should.equal('errors.utils.parsepackagejson.nameOrVersionMissing'); err.context.should.equal(tmpFile.name); - err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/'); + err.help.should.equal('errors.utils.parsepackagejson.willBeRequired'); done(); }) @@ -95,9 +101,9 @@ describe('lib/fs/package-json: parse', function () { done(new Error('packageJSON.parse succeeded, but should\'ve failed')); }) .catch(function (err) { - err.message.should.equal('Theme package.json file is malformed'); + err.message.should.equal('errors.utils.parsepackagejson.themeFileIsMalformed'); err.context.should.equal(tmpFile.name); - err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/api/handlebars-themes/'); + err.help.should.equal('errors.utils.parsepackagejson.willBeRequired'); done(); }) @@ -114,7 +120,7 @@ describe('lib/fs/package-json: parse', function () { done(new Error('packageJSON.parse succeeded, but should\'ve failed')); }) .catch(function (err) { - err.message.should.equal('Could not read package.json file'); + err.message.should.equal('errors.utils.parsepackagejson.couldNotReadPackage'); err.context.should.equal(tmpFile.name); done(); diff --git a/ghost/package-json/test/read_spec.js b/ghost/package-json/test/read_spec.js index 7fb29c8aee..2f2a83ce06 100644 --- a/ghost/package-json/test/read_spec.js +++ b/ghost/package-json/test/read_spec.js @@ -2,7 +2,13 @@ const should = require('should'); const tmp = require('tmp'); const join = require('path').join; const fs = require('fs-extra'); -const packageJSON = require('../../../../../core/server/lib/fs/package-json'); +const PackageJSON = require('../../../../../core/server/lib/fs/package-json/package-json'); + +const packageJSON = new PackageJSON({ + i18n: { + t: key => key + } +}); describe('lib/fs/package-json: read', function () { describe('all', function () { From 7f4d2bb06e3867d8b3837e21304fd9620086ae79 Mon Sep 17 00:00:00 2001 From: Aileen Nowak Date: Wed, 20 Jan 2021 09:59:45 +1300 Subject: [PATCH 23/27] Updated links to ghost.org sites no issue Follow-up task of the updated Ghost Docs structure. Updated links reflecting the new structure to prevent unnecessary 404s and redirects. --- ghost/package-json/lib/package-json.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js index e5b8d46e38..5f05e5520a 100644 --- a/ghost/package-json/lib/package-json.js +++ b/ghost/package-json/lib/package-json.js @@ -20,7 +20,7 @@ const packageJSONPath = 'package.json'; * - Adapters: replace fundamental pieces like storage, will become npm modules * * These utils facilitate loading, reading, managing etc, packages from the file system. - * + * */ module.exports = class PackageJson { /** @@ -97,7 +97,7 @@ module.exports = class PackageJson { const err = new Error(this.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); err.context = path; err.err = parseError; - err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); + err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/themes/'}); return Promise.reject(err); } @@ -107,7 +107,7 @@ module.exports = class PackageJson { if (!hasRequiredKeys) { const err = new Error(this.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); err.context = path; - err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/api/handlebars-themes/'}); + err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/themes/'}); return Promise.reject(err); } From 90ca836cb64a97b3b143b876a11b8d084bfd8d3c Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 27 Apr 2021 14:18:04 +0100 Subject: [PATCH 24/27] Expanded requires of lib/common i18n and events - Having these as destructured from the same package is hindering refactoring now - Events should really only ever be used server-side - i18n should be a shared module for now so it can be used everywhere until we figure out something better - Having them seperate also allows us to lint them properly --- ghost/package-json/lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js index 89b8232e17..4970e06f61 100644 --- a/ghost/package-json/lib/index.js +++ b/ghost/package-json/lib/index.js @@ -1,4 +1,4 @@ const PackageJson = require('./package-json'); -const {i18n} = require('../../common'); +const i18n = require('../../common/i18n'); module.exports = new PackageJson({i18n}); From e1b18aba2c755cc070b97d570b3f2dffd3a4a3a6 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 3 May 2021 17:29:44 +0100 Subject: [PATCH 25/27] Moved i18n to shared refs https://github.com/TryGhost/Ghost/commit/90ca836cb64a97b3b143b876a11b8d084bfd8d3c - i18n is used everywhere but only requires shared or external packages, therefore it's a good candidate for living in shared - this reduces invalid requires across frontend and server, and lets us use it everywhere until we come up with a better option --- ghost/package-json/lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js index 4970e06f61..bbae03b90c 100644 --- a/ghost/package-json/lib/index.js +++ b/ghost/package-json/lib/index.js @@ -1,4 +1,4 @@ const PackageJson = require('./package-json'); -const i18n = require('../../common/i18n'); +const i18n = require('../../../../shared/i18n'); module.exports = new PackageJson({i18n}); From eea93d55f40cf4e33e3d3453f7661ea3f6b83add Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Thu, 6 May 2021 12:46:37 +0100 Subject: [PATCH 26/27] Moved `package-json` wrapper outside implementation folder no issue - we're preparing the `package-json` lib to be extracted out of Ghost into its own package so moving the initialization wrapper outside of the folder makes the process a lot easier --- ghost/package-json/lib/index.js | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 ghost/package-json/lib/index.js diff --git a/ghost/package-json/lib/index.js b/ghost/package-json/lib/index.js deleted file mode 100644 index bbae03b90c..0000000000 --- a/ghost/package-json/lib/index.js +++ /dev/null @@ -1,4 +0,0 @@ -const PackageJson = require('./package-json'); -const i18n = require('../../../../shared/i18n'); - -module.exports = new PackageJson({i18n}); From 95cfa97747a0d047d58a5a84a9e4a2b1171a7025 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Thu, 6 May 2021 13:22:59 +0100 Subject: [PATCH 27/27] Changed `Error` to `IncorrectUsageError` in `package-json` no issue - `Error` is very generic for this case and `IncorrectUsageError` will populate the resulting error with the correct error code - the `message` was pulled out to its own statement so we can avoid long lines --- ghost/package-json/lib/package-json.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js index 5f05e5520a..213a2a1521 100644 --- a/ghost/package-json/lib/package-json.js +++ b/ghost/package-json/lib/package-json.js @@ -84,7 +84,8 @@ module.exports = class PackageJson { try { source = await fs.readFile(path); } catch (readError) { - const err = new Error(this.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage')); + const err = new errors.IncorrectUsageError(); + err.message = this.i18n.t('errors.utils.parsepackagejson.couldNotReadPackage'); err.context = path; err.err = readError; @@ -94,7 +95,8 @@ module.exports = class PackageJson { try { json = JSON.parse(source); } catch (parseError) { - const err = new Error(this.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed')); + const err = new errors.IncorrectUsageError(); + err.message = this.i18n.t('errors.utils.parsepackagejson.themeFileIsMalformed'); err.context = path; err.err = parseError; err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/themes/'}); @@ -105,7 +107,8 @@ module.exports = class PackageJson { const hasRequiredKeys = json.name && json.version; if (!hasRequiredKeys) { - const err = new Error(this.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing')); + const err = new errors.IncorrectUsageError(); + err.message = this.i18n.t('errors.utils.parsepackagejson.nameOrVersionMissing'); err.context = path; err.help = this.i18n.t('errors.utils.parsepackagejson.willBeRequired', {url: 'https://ghost.org/docs/themes/'});