Skip to content

Commit

Permalink
Remove lodash from create-pwa (#3003)
Browse files Browse the repository at this point in the history
* Remove lodash from create-pwa

* Fix bug

* Extend fix to venia-concept

* Using map instead of set.

* Pass sample backends into create script

* Fixup

* Restore sample backend export

* Fix unit test

Co-authored-by: Revanth Kumar <revanth0212@gmail.com>
  • Loading branch information
jimbo and revanth0212 authored Feb 10, 2021
1 parent 0573cfc commit c04a705
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
18 changes: 13 additions & 5 deletions packages/create-pwa/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ const chalk = require('chalk');
const gitUserInfo = require('git-user-info');
const isInvalidPath = require('is-invalid-path');
const isValidNpmName = require('is-valid-npm-name');
const { uniqBy } = require('lodash');

const pkg = require('../package.json');
const {
sampleBackends: defaultSampleBackends
} = require('@magento/pwa-buildpack/lib/cli/create-project');
const defaultSampleBackends = require('@magento/pwa-buildpack/sampleBackends.json');

const uniqBy = (array, property) => {
const map = new Map();

for (const element of array) {
if (element && element.hasOwnProperty(property)) {
map.set(element[property], element);
}
}

return Array.from(map.values());
};

const removeDuplicateBackends = backendEnvironments =>
uniqBy(backendEnvironments, 'url');
Expand Down
1 change: 0 additions & 1 deletion packages/create-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"inquirer": "^6.3.1",
"is-invalid-path": "^1.0.2",
"is-valid-npm-name": "^0.0.4",
"lodash": "~4.17.11",
"node-fetch": "~2.3.0",
"webpack": "^4.29.5"
}
Expand Down
4 changes: 3 additions & 1 deletion packages/pwa-buildpack/lib/Utilities/createProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const micromatch = require('micromatch');
const getBuildpackInstructions = require('./getBuildpackInstructions');
const fse = require('fs-extra');
const gitIgnoreToGlob = require('gitignore-to-glob');
const sampleBackends = require('../../sampleBackends.json');

const isMatch = (path, globs) => micromatch.isMatch(path, globs, { dot: true });

Expand Down Expand Up @@ -111,7 +112,8 @@ async function createProject(options) {
} = await instructions.create({
fs: fse,
tasks: makeCommonTasks(fse),
options
options,
sampleBackends
});
await before({ options });
await makeCopyStream({
Expand Down
3 changes: 2 additions & 1 deletion packages/pwa-buildpack/lib/cli/create-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const chalk = require('chalk');
const createProject = require('../Utilities/createProject');
const { handler: createEnvFile } = require('./create-env-file');
const execa = require('execa');
const sampleBackends = require('../../sampleBackends.json');

const tmpDir = os.tmpdir();

Expand Down Expand Up @@ -87,7 +88,7 @@ async function findTemplateDir(templateName) {
}
}

module.exports.sampleBackends = require('../../sampleBackends.json');
module.exports.sampleBackends = sampleBackends;

module.exports.command = 'create-project <directory>';

Expand Down
4 changes: 3 additions & 1 deletion packages/venia-concept/_buildpack/__tests__/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
makeCommonTasks,
makeCopyStream
} = require('@magento/pwa-buildpack/lib/Utilities/createProject');
const sampleBackends = require('@magento/pwa-buildpack/sampleBackends.json');
const createVenia = require('../create');

const mockFs = data => {
Expand Down Expand Up @@ -50,7 +51,8 @@ const runCreate = async (fs, opts) => {
const { after, before, visitor } = await createVenia({
fs,
tasks: makeCommonTasks(fs),
options
options,
sampleBackends
});
if (before) {
await before({ options });
Expand Down
22 changes: 15 additions & 7 deletions packages/venia-concept/_buildpack/create.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const { resolve } = require('path');
const { uniqBy } = require('lodash');
const {
sampleBackends: defaultSampleBackends
} = require('@magento/pwa-buildpack/lib/cli/create-project');

const uniqBy = (array, property) => {
const map = new Map();

for (const element of array) {
if (element && element.hasOwnProperty(property)) {
map.set(element[property], element);
}
}

return Array.from(map.values());
};

const removeDuplicateBackends = backendEnvironments =>
uniqBy(backendEnvironments, 'url');

const fetchSampleBackends = async () => {
const fetchSampleBackends = async defaultSampleBackends => {
try {
const res = await fetch(
'https://fvp0esmt8f.execute-api.us-east-1.amazonaws.com/default/getSampleBackends'
Expand All @@ -23,9 +31,9 @@ const fetchSampleBackends = async () => {
}
};

async function createProjectFromVenia({ fs, tasks, options }) {
async function createProjectFromVenia({ fs, tasks, options, sampleBackends }) {
const npmCli = options.npmClient;
const sampleBackendEnvironments = await fetchSampleBackends();
const sampleBackendEnvironments = await fetchSampleBackends(sampleBackends);

const toCopyFromPackageJson = [
'main',
Expand Down

0 comments on commit c04a705

Please sign in to comment.