Skip to content

Commit b61b1ad

Browse files
author
stpham
committed
docs: add code documention for some functions called in the index module
1 parent fc221be commit b61b1ad

15 files changed

Lines changed: 9522 additions & 50 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/node_modules/
2+
.idea/

src/choose-blueprint-updates.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const inquirer = require('inquirer');
55
const loadSafeBlueprint = require('./load-safe-blueprint');
66
const { defaultTo } = require('./constants');
77

8+
/**
9+
* Format the string that is displayed when user is prompted for a blueprint
10+
*
11+
* @param {object} blueprint - Expected to contain `name` and `version` attributes
12+
* @param {string} latestVersion - Latest version for the blueprint
13+
* @returns {string}
14+
*/
815
function formatBlueprintLine({
916
blueprint,
1017
latestVersion
@@ -26,6 +33,17 @@ async function chooseBlueprint({
2633
return choicesByName[answer.blueprint];
2734
}
2835

36+
/**
37+
* Facilitate prompting the user for which bllueprint that want to update
38+
*
39+
* @param cwd
40+
* @param emberCliUpdateJson
41+
* @param reset
42+
* @param compare
43+
* @param codemods
44+
* @param to
45+
* @returns {Promise<{blueprint: (*|{}), areAllUpToDate, to: string}>}
46+
*/
2947
async function chooseBlueprintUpdates({
3048
cwd,
3149
emberCliUpdateJson,

src/get-base-blueprint.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ const downloadPackage = require('./download-package');
55
const loadSafeBlueprint = require('./load-safe-blueprint');
66
const isDefaultBlueprint = require('./is-default-blueprint');
77

8+
/**
9+
* If the passed in `blueprint` is not the base blueprint, find it. A base blueprint is either
10+
* an `addon`, `app`, or `glimmer` blueprint or has the `isBaseBlueprint` boolean set to true.
11+
*
12+
* @param cwd - Used in `parseBlueprintPackage` to read package.json and find a viable version and normalize url
13+
* if it exists
14+
* @param blueprints - Find the base blueprint in this array if the passed blueprint is not one
15+
* @param blueprint - Figure out if this is a base blueprint
16+
* @returns {Promise<*>}
17+
*/
818
async function getBaseBlueprint({
919
cwd,
1020
blueprints,

src/get-blueprint-file-path.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ async function getBlueprintFilePath(cwd) {
99
return path.join(cwd, relative);
1010
}
1111

12+
/**
13+
* Use `config` as the parent directory of `ember-cli-update.json` unless a different config
14+
* folder is set in the a `package.json` file at the root of the project directory
15+
*
16+
* @param {String} cwd - Current working directory path where a `package.json` file could exist
17+
* @returns {Promise<string>}
18+
*/
1219
async function getBlueprintRelativeFilePath(cwd) {
1320
let configDir = 'config';
1421

src/get-project-options.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const path = require('path');
44
const fs = require('fs-extra');
55
const isDefaultBlueprint = require('./is-default-blueprint');
66

7+
/**
8+
* Determine if project is a `addon`, `app`, or `glimmer` type
9+
*
10+
* @param {function} checkForDep - Function that will check if dependency exists in `devDependencies` or `dependencies`
11+
* attribute of `package.json`
12+
* @param {array} keywords - Array of strings of the `keywords` attribute from `package.json`
13+
* @returns {string}
14+
*/
715
function getProjectType(checkForDep, keywords) {
816
let isAddon = keywords && keywords.indexOf('ember-addon') !== -1;
917

@@ -26,6 +34,15 @@ function getProjectType(checkForDep, keywords) {
2634
throw new Error('Ember CLI project type could not be determined');
2735
}
2836

37+
/**
38+
* Determine what kind of ember flavor this project is and if it uses yarn or npm
39+
*
40+
* @param keywords
41+
* @param dependencies
42+
* @param devDependencies
43+
* @param blueprint
44+
* @returns {Promise<[string]|string[]>} - Array of strings containing keywords
45+
*/
2946
module.exports = async function getProjectOptions({
3047
keywords,
3148
dependencies,

src/get-project-version.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ const _getProjectVersion = require('boilerplate-update/src/get-project-version')
55

66
const glimmerVersionCutoff = '0.6.3';
77

8+
/**
9+
* Fine the minimum version from `versions` array that matches the `packageVersion` range string
10+
*
11+
* @param {string} packageVersion - Can be a version range such as ^1.0.2 or an exact version
12+
* @param {array<string>} versions - Array of version strings available for package
13+
* @param {object} projectOptions - Glimmer projects are a special case
14+
* @returns {string}
15+
*/
816
module.exports = function getProjectVersion(packageVersion, versions, projectOptions) {
17+
// _getProjectVersion gets the minimum version that satisfies the given packageVersion string
918
let projectVersion = _getProjectVersion(packageVersion, versions);
1019

1120
if (projectOptions.includes('glimmer') && semver.lt(projectVersion, glimmerVersionCutoff)) {

src/get-versions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
const npm = require('boilerplate-update/src/npm');
44

5+
/**
6+
* Returns an array of strings i.e.
7+
* [
8+
* "0.0.0",
9+
* "3.23.0-beta.2"
10+
* ]
11+
* @param packageName
12+
* @returns {Promise<Array<string>>}
13+
*/
514
module.exports = async function getVersions(packageName) {
615
return await npm.json(`view ${packageName} versions`);
716
};

src/index.js

Lines changed: 96 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,82 @@ const resolvePackage = require('./resolve-package');
2323
const { defaultTo } = require('./constants');
2424
const normalizeBlueprintArgs = require('./normalize-blueprint-args');
2525

26+
/**
27+
* Build an object of configurations based on contents of the package containing the blueprint specified
28+
* by the user
29+
*/
30+
async function setupParamPassedBlueprint(
31+
selectedPackageName,
32+
selectedBlueprintName,
33+
emberCliUpdateJson,
34+
fromVersion,
35+
cwd
36+
) {
37+
let blueprintArgs = normalizeBlueprintArgs({
38+
selectedPackageName,
39+
blueprintName: selectedBlueprintName
40+
});
41+
let parsedPackage = await parseBlueprintPackage({
42+
cwd,
43+
packageName: blueprintArgs.packageName
44+
});
45+
let blueprint;
46+
let packageUrl = parsedPackage.url;
47+
let packageName = parsedPackage.name;
48+
49+
if (!packageName) {
50+
let downloadedPackage = await downloadPackage(null, packageUrl, defaultTo);
51+
packageName = downloadedPackage.name;
52+
}
53+
54+
let blueprintName;
55+
if (blueprintArgs.blueprintName !== blueprintArgs.packageName) {
56+
blueprintName = blueprintArgs.blueprintName;
57+
} else {
58+
blueprintName = packageName;
59+
}
60+
61+
let existingBlueprint = findBlueprint(emberCliUpdateJson, packageName, blueprintName);
62+
63+
if (existingBlueprint) {
64+
blueprint = existingBlueprint;
65+
} else {
66+
blueprint = loadSafeBlueprint({
67+
packageName,
68+
name: blueprintName,
69+
location: parsedPackage.location
70+
});
71+
72+
if (isDefaultBlueprint(blueprint)) {
73+
blueprint = await loadDefaultBlueprintFromDisk({
74+
cwd,
75+
version: fromVersion
76+
});
77+
}
78+
}
79+
80+
if (fromVersion) {
81+
blueprint.version = fromVersion;
82+
}
83+
84+
if (!blueprint.version) {
85+
throw new Error('A custom blueprint cannot detect --from. You must supply it.');
86+
}
87+
88+
return blueprint;
89+
}
90+
91+
/**
92+
* If `version` attribute exists in the `blueprint` object and URL is empty, skip. Otherwise resolve the details of
93+
* the blueprint
94+
*
95+
* @param {Object} blueprint - Expected to contain `name`, `options` array, `packageName`, `location`, and `version`
96+
* attributes
97+
* @param {String} url - Optional parameter that links to package
98+
* @param {String} range - Version range i.e. 1.0.2
99+
* @returns {Promise<void>}
100+
* @private
101+
*/
26102
async function _resolvePackage(blueprint, url, range) {
27103
if (blueprint.version && !url) {
28104
return;
@@ -44,6 +120,16 @@ async function _resolvePackage(blueprint, url, range) {
44120
}
45121
}
46122

123+
/**
124+
*
125+
* @param cwd
126+
* @param packageName - User passed package name
127+
* @param _blueprint - User passed blueprint name
128+
* @param from
129+
* @param to
130+
* @param resolveConflicts
131+
* @returns {Promise<{promise: Promise<*>, resolveConflictsProcess}|{promise: Promise<void>}>}
132+
*/
47133
module.exports = async function emberCliUpdate({
48134
cwd = process.cwd(),
49135
packageName,
@@ -65,54 +151,13 @@ module.exports = async function emberCliUpdate({
65151
let packageUrl;
66152

67153
if (_blueprint) {
68-
let blueprintArgs = normalizeBlueprintArgs({
154+
blueprint = await setupParamPassedBlueprint(
69155
packageName,
70-
blueprintName: _blueprint
71-
});
72-
73-
let parsedPackage = await parseBlueprintPackage({
74-
cwd,
75-
packageName: blueprintArgs.packageName
76-
});
77-
packageUrl = parsedPackage.url;
78-
79-
packageName = parsedPackage.name;
80-
if (!packageName) {
81-
let downloadedPackage = await downloadPackage(null, packageUrl, defaultTo);
82-
packageName = downloadedPackage.name;
83-
}
84-
let blueprintName;
85-
if (blueprintArgs.blueprintName !== blueprintArgs.packageName) {
86-
blueprintName = blueprintArgs.blueprintName;
87-
} else {
88-
blueprintName = packageName;
89-
}
90-
91-
let existingBlueprint = findBlueprint(emberCliUpdateJson, packageName, blueprintName);
92-
if (existingBlueprint) {
93-
blueprint = existingBlueprint;
94-
} else {
95-
blueprint = loadSafeBlueprint({
96-
packageName,
97-
name: blueprintName,
98-
location: parsedPackage.location
99-
});
100-
101-
if (isDefaultBlueprint(blueprint)) {
102-
blueprint = await loadDefaultBlueprintFromDisk({
103-
cwd,
104-
version: from
105-
});
106-
}
107-
}
108-
109-
if (from) {
110-
blueprint.version = from;
111-
}
112-
113-
if (!blueprint.version) {
114-
throw new Error('A custom blueprint cannot detect --from. You must supply it.');
115-
}
156+
_blueprint,
157+
emberCliUpdateJson,
158+
from,
159+
cwd
160+
);
116161
} else if (blueprints.length) {
117162
let {
118163
areAllUpToDate,
@@ -137,6 +182,7 @@ module.exports = async function emberCliUpdate({
137182
});
138183
}
139184

185+
// If blueprint is located on disk
140186
if (blueprint.location && !packageUrl) {
141187
let parsedPackage = await parseBlueprintPackage({
142188
cwd,
@@ -153,6 +199,9 @@ module.exports = async function emberCliUpdate({
153199
blueprint
154200
});
155201

202+
// If no base blueprint is found, set the selected one as the base blueprint.
203+
// `glimmer`, `app`, and `addon` blueprints as well as ones whose `isBaseBlueprint` attribute is
204+
// set to true will also have baseBlueprint undefined
156205
if (!baseBlueprint) {
157206
// for non-existing blueprints
158207
blueprint.isBaseBlueprint = true;

src/is-default-blueprint.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ const {
77
glimmerPackageName
88
} = require('./constants');
99

10+
/**
11+
* A default blueprint either the `addon` or `app` blueprint provided by `ember-cli` or the `glimmer` one
12+
*
13+
* @param {string} packageName - Ensure this is one of the package names that contain the default blueprints
14+
* @param {string} name - Check if this is the name of one of the default blueprint names
15+
* @returns {boolean}
16+
*/
1017
function isDefaultBlueprint({ packageName, name }) {
1118
if (packageName === glimmerPackageName && name === glimmerPackageName) {
1219
return true;

src/load-default-blueprint-from-disk.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ const getProjectVersion = require('./get-project-version');
88
const loadDefaultBlueprint = require('./load-default-blueprint');
99
const utils = require('./utils');
1010

11+
/**
12+
* Generate the configuration for the "base blueprint" which is the blueprint that first created
13+
* the ember project
14+
*
15+
* @param {string} cwd - Current working directory expected to be a node project path
16+
* @param {string} version - Optional. if not pass will use the one specified in package json
17+
* @returns {Promise<*|{}>}
18+
*/
1119
async function loadDefaultBlueprintFromDisk({
1220
cwd,
1321
version
@@ -32,9 +40,7 @@ async function loadDefaultBlueprintFromDisk({
3240
}
3341
}
3442

35-
let blueprint = loadDefaultBlueprint(projectOptions, version);
36-
37-
return blueprint;
43+
return loadDefaultBlueprint(projectOptions, version);
3844
}
3945

4046
module.exports = loadDefaultBlueprintFromDisk;

0 commit comments

Comments
 (0)