gscan 4.9.2 → 4.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/checker.js CHANGED
@@ -41,7 +41,7 @@ const checker = function checkAll(themePath, options = {}) {
41
41
  }
42
42
  });
43
43
 
44
- return readTheme(themePath, options)
44
+ return readTheme(themePath)
45
45
  .then(function (theme) {
46
46
  // set the major version to check
47
47
  theme.checkedVersion = versions[version].major;
@@ -1,17 +1,12 @@
1
1
  const _ = require('lodash');
2
2
  const spec = require('../specs');
3
- const versions = require('../utils').versions;
4
- const packageJSONFileName = 'package.json';
3
+ const {versions, getPackageJSON} = require('../utils');
5
4
 
6
5
  module.exports = function checkUsage(theme, options) {
7
6
  const checkVersion = _.get(options, 'checkVersion', versions.default);
8
7
  let ruleSet = spec.get([checkVersion]);
9
- let [packageJSON] = _.filter(theme.files, {file: packageJSONFileName});
10
- let targetApiVersion = versions.default;
11
- if (packageJSON && packageJSON.content) {
12
- let packageJSONParsed = JSON.parse(packageJSON.content);
13
- targetApiVersion = (packageJSONParsed && packageJSONParsed.engines && packageJSONParsed.engines['ghost-api']) || versions.default;
14
- }
8
+ const packageJSON = getPackageJSON(theme);
9
+ let targetApiVersion = (packageJSON && packageJSON.engines && packageJSON.engines['ghost-api']) || versions.default;
15
10
 
16
11
  // CASE: 080-helper-usage checks only needs `rules` that start with `GS080-`
17
12
  const ruleRegex = /GS080-.*/g;
@@ -1,6 +1,6 @@
1
1
  const _ = require('lodash');
2
2
  const spec = require('../specs');
3
- const {versions, normalizePath} = require('../utils');
3
+ const {versions, normalizePath, getPackageJSON} = require('../utils');
4
4
  const ASTLinter = require('../ast-linter');
5
5
 
6
6
  function processFileFunction(files, failures, rules) {
@@ -40,13 +40,10 @@ function processFileFunction(files, failures, rules) {
40
40
  }
41
41
 
42
42
  function getCustomThemeSettings(theme) {
43
- let [packageJSON] = _.filter(theme.files, {file: 'package.json'});
43
+ const packageJSON = getPackageJSON(theme);
44
44
  let customThemeSettingsConfig;
45
- if (packageJSON && packageJSON.content) {
46
- let packageJSONParsed = JSON.parse(packageJSON.content);
47
- if (packageJSONParsed.config && packageJSONParsed.config.custom) {
48
- customThemeSettingsConfig = packageJSONParsed.config.custom;
49
- }
45
+ if (packageJSON && packageJSON.config && packageJSON.config.custom) {
46
+ customThemeSettingsConfig = packageJSON.config.custom;
50
47
  }
51
48
  return customThemeSettingsConfig;
52
49
  }
@@ -116,15 +116,8 @@ function parseWithAST({theme, log, file, rules, callback}){
116
116
 
117
117
  const ruleImplementations = {
118
118
  'GS100-NO-UNUSED-CUSTOM-THEME-SETTING': {
119
- isEnabled: ({theme, result}) => {
120
- let [packageJSON] = _.filter(theme.files, {file: 'package.json'});
121
- if (packageJSON && packageJSON.content) {
122
- let packageJSONParsed = JSON.parse(packageJSON.content);
123
- if (packageJSONParsed.config && packageJSONParsed.config.custom) {
124
- result.customThemeSettingsConfig = packageJSONParsed.config.custom;
125
- }
126
- }
127
- return !!result.customThemeSettingsConfig;
119
+ isEnabled: ({theme}) => {
120
+ return !!theme.customSettings;
128
121
  },
129
122
  init: ({result}) => {
130
123
  result.customThemeSettings = new Set();
@@ -142,8 +135,8 @@ const ruleImplementations = {
142
135
  }});
143
136
  }
144
137
  },
145
- done: ({log, result}) => {
146
- const config = Object.keys(result.customThemeSettingsConfig);
138
+ done: ({log, theme, result}) => {
139
+ const config = Object.keys(theme.customSettings);
147
140
  const notUsedVariable = config.filter(x => !result.customThemeSettings.has(x));
148
141
 
149
142
  if (notUsedVariable.length > 0) {
package/lib/read-theme.js CHANGED
@@ -75,11 +75,9 @@ const readThemeStructure = function readThemeFiles(themePath, subPath, arr) {
75
75
  /**
76
76
  *
77
77
  * @param {Theme} theme
78
- * @param {Object} options
79
- * @param {Object=} [options.labs] object containing boolean flags for enabled labs features
80
78
  * @returns {Promise<Theme>}
81
79
  */
82
- const readFiles = function readFiles(theme, options = {}) {
80
+ const readFiles = function readFiles(theme) {
83
81
  const themeFilesContent = _.filter(theme.files, function (themeFile) {
84
82
  if (themeFile && themeFile.ext) {
85
83
  return themeFile.ext.match(/\.hbs|\.css|\.js/ig) || themeFile.file.match(/package.json/i);
@@ -98,17 +96,19 @@ const readFiles = function readFiles(theme, options = {}) {
98
96
  return fs.readFile(path.join(theme.path, themeFile.file), 'utf8').then(function (content) {
99
97
  themeFile.content = content;
100
98
 
101
- if (options.labs && options.labs.customThemeSettings) {
102
- if (!theme.customSettings) {
103
- theme.customSettings = {};
104
- }
99
+ if (!theme.customSettings) {
100
+ theme.customSettings = {};
101
+ }
105
102
 
106
- const packageJsonMatch = themeFile.file.match(/^package\.json/);
107
- if (packageJsonMatch) {
103
+ const packageJsonMatch = themeFile.file === 'package.json';
104
+ if (packageJsonMatch) {
105
+ try {
108
106
  const packageJson = JSON.parse(themeFile.content);
109
107
  if (packageJson.config && packageJson.config.custom) {
110
108
  theme.customSettings = packageJson.config.custom;
111
109
  }
110
+ } catch (e) {
111
+ // Ignore error as they will be caught in 010-package-json.js
112
112
  }
113
113
  }
114
114
 
@@ -230,11 +230,9 @@ const extractTemplates = function extractTemplates(allFiles) {
230
230
  /**
231
231
  *
232
232
  * @param {string} themePath - path to the validated theme
233
- * @param {Object} options
234
- * @param {Object=} [options.labs] object containing boolean flags for enabled labs features
235
233
  * @returns {Promise<Theme>}
236
234
  */
237
- module.exports = function readTheme(themePath, options = {}) {
235
+ module.exports = function readTheme(themePath) {
238
236
  return readThemeStructure(themePath)
239
237
  .then(function (themeFiles) {
240
238
  var allTemplates = extractTemplates(themeFiles);
@@ -252,7 +250,7 @@ module.exports = function readTheme(themePath, options = {}) {
252
250
  pass: [],
253
251
  fail: {}
254
252
  }
255
- }, options);
253
+ });
256
254
  });
257
255
  };
258
256
 
@@ -16,5 +16,6 @@ module.exports = {
16
16
  // `docs` is used to generate the URLs that link to documentation and needs to be updated whenever
17
17
  // we release a new version on ghost.org/docs/api/
18
18
  versions: require('./versions.json'),
19
- normalizePath
19
+ normalizePath,
20
+ getPackageJSON: require('./package-json')
20
21
  };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Extracts the package.json JSON content. Note that this function never throws,
3
+ * even when there is a JSON parsing error.
4
+ * @param {Object} theme The theme to extract package.json from.
5
+ * @returns {Object} The content of the package.json file, or `null` if
6
+ * something happened (no file, JSON parsing error...).
7
+ */
8
+ function getJSON(theme) {
9
+ let packageJSON = theme.files.find(item => item.file === 'package.json');
10
+ if (packageJSON && packageJSON.content) {
11
+ try {
12
+ return JSON.parse(packageJSON.content);
13
+ } catch (e) {
14
+ // Do nothing here
15
+ }
16
+ }
17
+ return null;
18
+ }
19
+
20
+ module.exports = getJSON;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gscan",
3
- "version": "4.9.2",
3
+ "version": "4.10.1",
4
4
  "description": "Scans Ghost themes looking for errors, deprecations, features and compatibility",
5
5
  "keywords": [
6
6
  "ghost",
@@ -14,7 +14,7 @@
14
14
  "url": "git@github.com:TryGhost/gscan.git"
15
15
  },
16
16
  "engines": {
17
- "node": "^12.10.0 || ^14.13.0"
17
+ "node": "^12.22.1 || ^14.17.0 || ^16.13.0"
18
18
  },
19
19
  "bugs": {
20
20
  "url": "https://github.com/TryGhost/gscan/issues"
@@ -60,7 +60,7 @@
60
60
  "validator": "13.0.0"
61
61
  },
62
62
  "devDependencies": {
63
- "eslint": "7.25.0",
63
+ "eslint": "8.1.0",
64
64
  "eslint-plugin-ghost": "2.1.0",
65
65
  "istanbul": "0.4.5",
66
66
  "mocha": "9.0.2",