-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V4P4: feat(buildpack): scaffolding creation (#1500)
* feat(buildpack): scaffolding creation. Closes #1693. test: add tests for createProject feat: CLI wizard for npm init @magento/pwa fix: scaffolding fixes and docs chore: remove lockfiles until we can maintain them Revise scaffolding docs (#1635) * Revise scaffolding overview docs * Revise create-project reference topic * Revise custom project template draft topic * Make changes based on PR review fixup: make create-pwa messages less venia-specific Co-Authored-By: Andy Terranova <13182778+supernova-at@users.noreply.github.com> fixup: log message and doc feedback Co-Authored-By: Andy Terranova <13182778+supernova-at@users.noreply.github.com> fixup: fix tests fixup prettier updates fixup debug mode in buildpack create * fixup remove is-valid-npm-name * enhancement: better post-install logging
- Loading branch information
1 parent
56fff7b
commit baa1e00
Showing
46 changed files
with
1,728 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('../')(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
const { basename, resolve } = require('path'); | ||
const os = require('os'); | ||
const changeCase = require('change-case'); | ||
const inquirer = require('inquirer'); | ||
const execa = require('execa'); | ||
const chalk = require('chalk'); | ||
const gitUserInfo = require('git-user-info'); | ||
const isInvalidPath = require('is-invalid-path'); | ||
const isValidNpmName = require('is-valid-npm-name'); | ||
const pkg = require('../package.json'); | ||
const { | ||
sampleBackends | ||
} = require('@magento/pwa-buildpack/lib/cli/create-project'); | ||
|
||
module.exports = async () => { | ||
console.log(chalk.greenBright(`${pkg.name} v${pkg.version}`)); | ||
console.log( | ||
chalk.white(`Creating a ${chalk.whiteBright('PWA Studio')} project`) | ||
); | ||
const userAgent = process.env.npm_config_user_agent || ''; | ||
const isYarn = userAgent.includes('yarn'); | ||
|
||
const questions = [ | ||
{ | ||
name: 'directory', | ||
message: | ||
'Project root directory (will be created if it does not exist)', | ||
validate: dir => | ||
!dir | ||
? 'Please enter a directory path' | ||
: isInvalidPath(dir) | ||
? 'Invalid directory path; contains illegal characters' | ||
: true | ||
}, | ||
{ | ||
name: 'name', | ||
message: | ||
'Short name of the project to put in the package.json "name" field', | ||
validate: isValidNpmName, | ||
default: ({ directory }) => basename(directory) | ||
}, | ||
{ | ||
name: 'author', | ||
message: | ||
'Name of the author to put in the package.json "author" field', | ||
default: () => { | ||
const userInfo = os.userInfo(); | ||
let author = userInfo.username; | ||
const gitInfo = gitUserInfo({ | ||
path: resolve(userInfo.homedir, '.gitconfig') | ||
}); | ||
|
||
if (gitInfo) { | ||
author = gitInfo.name || author; | ||
if (gitInfo.email) { | ||
author += ` <${gitInfo.email}>`; | ||
} | ||
} | ||
return author; | ||
} | ||
}, | ||
{ | ||
name: 'backendUrl', | ||
type: 'list', | ||
message: | ||
'Magento instance to use as a backend (will be added to `.env` file)', | ||
choices: sampleBackends.environments | ||
.map(({ name, description, url }) => ({ | ||
name: description, | ||
value: url, | ||
short: name | ||
})) | ||
.concat([ | ||
{ | ||
name: | ||
'Other (I will provide my own backing Magento instance)', | ||
value: false, | ||
short: 'Other' | ||
} | ||
]) | ||
}, | ||
{ | ||
name: 'customBackendUrl', | ||
message: | ||
'URL of a Magento instance to use as a backend (will be added to `.env` file)', | ||
default: 'https://magento2.localhost', | ||
when: ({ backendUrl }) => !backendUrl | ||
}, | ||
{ | ||
name: 'npmClient', | ||
type: 'list', | ||
message: 'NPM package management client to use', | ||
choices: ['npm', 'yarn'], | ||
default: isYarn ? 'yarn' : 'npm' | ||
}, | ||
{ | ||
name: 'install', | ||
type: 'confirm', | ||
message: ({ npmClient }) => | ||
`Install package dependencies with ${npmClient} after creating project`, | ||
default: true | ||
} | ||
]; | ||
let answers; | ||
try { | ||
answers = await inquirer.prompt(questions); | ||
} catch (e) { | ||
console.error('App creation cancelled.'); | ||
} | ||
answers.backendUrl = answers.backendUrl || answers.customBackendUrl; | ||
const args = questions.reduce( | ||
(args, q) => { | ||
if (q.name === 'customBackendUrl' || q.name === 'directory') { | ||
return args; | ||
} | ||
const answer = answers[q.name]; | ||
const option = changeCase.paramCase(q.name); | ||
if (q.type === 'confirm') { | ||
if (answer !== q.default) { | ||
return [...args, answer ? `--${option}` : `--no-${option}`]; | ||
} | ||
return args; | ||
} | ||
return [...args, `--${option}`, `"${answer}"`]; | ||
}, | ||
['create-project', answers.directory, '--template', '"venia-concept"'] | ||
); | ||
|
||
const argsString = args.join(' '); | ||
|
||
console.log( | ||
'\nRunning command: \n\n' + | ||
chalk.whiteBright(`buildpack ${argsString}\n\n`) | ||
); | ||
|
||
const buildpackBinLoc = resolve( | ||
require.resolve('@magento/pwa-buildpack'), | ||
'../../bin/buildpack' | ||
); | ||
await execa.shell(`${buildpackBinLoc} ${argsString}`, { | ||
stdio: 'inherit' | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "@magento/create-pwa", | ||
"version": "1.0.0", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Create a PWA Studio app with a single command.", | ||
"main": "./lib/index.js", | ||
"bin": "./bin/create-pwa", | ||
"scripts": { | ||
"build": " ", | ||
"build:ci": " ", | ||
"build:dev": " ", | ||
"clean": " ", | ||
"test": " " | ||
}, | ||
"repository": "github:magento-research/pwa-studio", | ||
"keywords": [ | ||
"magento", | ||
"pwa", | ||
"create-pwa", | ||
"create-pwa-app", | ||
"pwa-studio" | ||
], | ||
"author": "Magento Commerce", | ||
"license": "(OSL-3.0 OR AFL-3.0)", | ||
"bugs": { | ||
"url": "https://github.com/magento-research/pwa-studio/issues" | ||
}, | ||
"homepage": "https://github.com/magento-research/pwa-studio/tree/master/packages/create-pwa#readme", | ||
"dependencies": { | ||
"@magento/pwa-buildpack": "~3.0.0", | ||
"chalk": "^2.4.2", | ||
"change-case": "^3.1.0", | ||
"execa": "^1.0.0", | ||
"git-user-info": "^1.0.1", | ||
"inquirer": "^6.3.1", | ||
"is-invalid-path": "^1.0.2", | ||
"is-valid-npm-name": "^0.0.4", | ||
"webpack": "^4.29.5" | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
.../Utilities/__tests__/__fixtures__/modules/buildpack-template-package/_buildpack/create.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const mock = { | ||
after: jest.fn(), | ||
visitor: { | ||
'index.js': jest.fn(), | ||
'**/*.css': jest.fn(), | ||
'ignoreexp/*': jest.fn() | ||
} | ||
}; | ||
|
||
const factory = () => mock; | ||
|
||
factory.mock = mock; | ||
|
||
module.exports = factory; |
5 changes: 5 additions & 0 deletions
5
...k/lib/Utilities/__tests__/__fixtures__/modules/buildpack-template-package/_gitignore_test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
_gitignore_test | ||
ignorefile | ||
ignoreexp/*.txt | ||
ignoredir | ||
nonexistentfile |
Empty file.
Empty file.
1 change: 1 addition & 0 deletions
1
...ldpack/lib/Utilities/__tests__/__fixtures__/modules/buildpack-template-package/ignorefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ignore me |
1 change: 1 addition & 0 deletions
1
...uildpack/lib/Utilities/__tests__/__fixtures__/modules/buildpack-template-package/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('buildpack-template-package index.js'); |
12 changes: 12 additions & 0 deletions
12
...pack/lib/Utilities/__tests__/__fixtures__/modules/buildpack-template-package/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "buildpack-template-package", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
3 changes: 3 additions & 0 deletions
3
...Utilities/__tests__/__fixtures__/modules/buildpack-template-package/stylesheets/index.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
html { | ||
display: none; | ||
} |
3 changes: 3 additions & 0 deletions
3
..._/__fixtures__/modules/buildpack-template-package/stylesheets/other-stylesheets/other.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* { | ||
display: block; | ||
} |
3 changes: 3 additions & 0 deletions
3
..._tests__/__fixtures__/modules/package-with-bad-instruction/_buildpack/throw-on-execute.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = () => { | ||
throw new Error('Failed to run a bad instruction'); | ||
}; |
Empty file.
12 changes: 12 additions & 0 deletions
12
...ck/lib/Utilities/__tests__/__fixtures__/modules/package-with-bad-instruction/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "package-with-bad-instruction", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
1 change: 1 addition & 0 deletions
1
...sts__/__fixtures__/modules/package-with-instruction/_buildpack/return-fse-readjsonsync.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = ({ fs }) => fs.readJsonSync; |
Empty file.
12 changes: 12 additions & 0 deletions
12
...ldpack/lib/Utilities/__tests__/__fixtures__/modules/package-with-instruction/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "package-with-instruction", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
Empty file.
12 changes: 12 additions & 0 deletions
12
...ck/lib/Utilities/__tests__/__fixtures__/modules/package-with-no-instructions/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "package-with-no-instructions", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC" | ||
} |
1 change: 1 addition & 0 deletions
1
...__/__fixtures__/non-package-folder-with-instruction/_buildpack/return-fse-readjsonsync.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = ({ fs }) => fs.readJsonSync; |
Oops, something went wrong.