Skip to content

Commit

Permalink
Add new Init command for Project Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
thafryer committed Dec 20, 2023
1 parent fa2d355 commit 0e059fa
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 6 deletions.
82 changes: 82 additions & 0 deletions bin-src/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import boxen from "boxen";
import {writeFile} from 'jsonfile';
import prompts from 'prompts';
import readPkgUp from 'read-pkg-up';

import type { Configuration } from "../node-src/types";
import noPackageJson from '../node-src/ui/messages/errors/noPackageJson';


const TestFrameworkType = {
STORYBOOK: 'storybook',
PLAYWRIGHT: 'playwright',
CYPRESS: 'cypress'
};

const addChromaticScriptToPackageJson = async (packageJson, packagePath) => {
try {
const scriptName = 'chromaticTest';
const json = { ...packageJson };
if (!json.scripts) json.scripts = {};
json.scripts[scriptName] = `npx chromatic`;
await writeFile(packagePath, json, { spaces: 2 });
} catch (e) {
console.warn(e)
}
}

const createChromaticConfigFile = async (configFile: string, configuration: Configuration) => {
await writeFile(configFile, configuration);
}

export async function main() {
console.log(
boxen('Welcome to Chromatic Initialization tool! This CLI will help get Chromatic setup within your project.', {
title: 'Chromatic Init',
titleAlignment: 'center',
textAlignment: 'center',
padding: 1,
borderStyle: 'double',
borderColor: '#FF4400',
})
)

const pkgInfo = await readPkgUp({ cwd: process.cwd() });
if (!pkgInfo) {
console.error(noPackageJson());
process.exit(253);
}

const { path: packagePath, packageJson } = pkgInfo;

const { buildScriptName} = await prompts([
{
type: 'select',
name: 'testFramework',
message: 'What testing framework are you using?',
choices: [
{title: 'Storybook', value: TestFrameworkType.STORYBOOK},
{title: 'Playwright', value: TestFrameworkType.PLAYWRIGHT},
{title: 'Cypress', value: TestFrameworkType.CYPRESS},
],
initial: 0
},
{
type: (prev) => prev === TestFrameworkType.STORYBOOK ? "text" : null,
name: 'buildScriptName',
message: "What is the name of the NPM script that builds your Storybook? (default: build-storybook)",
}
])
const {readme, _id, ...rest} = packageJson
await addChromaticScriptToPackageJson(rest, packagePath);
await createChromaticConfigFile('chromatic.config.json', {
autoAcceptChanges: "main",
...(buildScriptName && {
buildScriptName
}),
// exitOnceUploaded: true, TODO: Only enable this option by default if project is linked.
externals: ["public/**"],
onlyChanged: true,
skip: "dependabot/**"
})
}
1 change: 1 addition & 0 deletions bin-src/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require('dotenv').config();

const commands = {
init: () => require('./init').main(process.argv.slice(3)),
main: () => require('./main').main(process.argv.slice(2)),
trace: () => require('./trace').main(process.argv.slice(3)),
'trim-stats-file': () => require('./trim-stats-file').main(process.argv.slice(3)),
Expand Down
2 changes: 1 addition & 1 deletion node-src/lib/checkPackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function checkPackageJson({
if (!options.interactive) return;

try {
const json = { ...packageJson };
const { readme, _id, ...json } = packageJson;
if (!json.scripts) json.scripts = {};
if (findScript(json.scripts)) return;

Expand Down
1 change: 1 addition & 0 deletions node-src/lib/getConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const configurationSchema = z
exitZeroOnChanges: z.union([z.string(), z.boolean()]),
exitOnceUploaded: z.union([z.string(), z.boolean()]),
ignoreLastBuildOnBranch: z.string(),
skip: z.union([z.string(), z.boolean()]),

buildScriptName: z.string(),
outputDir: z.string(),
Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
},
"repository": {
"type": "git",
"url": "https://github.com/chromaui/chromatic-cli.git"
"url": "git+https://github.com/chromaui/chromatic-cli.git"
},
"license": "MIT",
"author": "Chromatic <support@chromatic.com>",
"author": {
"name": "Chromatic",
"email": "support@chromatic.com"
},
"exports": {
".": {
"types": "./dist/node.d.ts",
Expand Down Expand Up @@ -84,7 +87,8 @@
"test": "vitest run && vitest run -c vitest.no-threads.config.ts",
"prepare": "husky install && npm run build",
"dev": "tsup --watch",
"lint-staged": "lint-staged"
"lint-staged": "lint-staged",
"chromaticTest": "npx chromatic"
},
"lint-staged": {
"*.ts": [
Expand Down Expand Up @@ -125,6 +129,7 @@
"@types/node": "18.x",
"@types/picomatch": "^2.3.0",
"@types/progress-stream": "^2.0.2",
"@types/prompts": "^2.4.9",
"@types/semver": "^7.3.9",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
Expand All @@ -133,6 +138,7 @@
"archiver": "^5.3.0",
"async-retry": "^1.3.3",
"auto": "^11.0.4",
"boxen": "^7.1.1",
"chalk": "^4.1.2",
"cpy": "^8.1.2",
"cross-env": "^7.0.3",
Expand Down Expand Up @@ -171,6 +177,7 @@
"pluralize": "^8.0.0",
"prettier": "^2.3.2",
"progress-stream": "^2.0.0",
"prompts": "^2.4.2",
"prop-types": "^15.7.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
103 changes: 101 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3495,6 +3495,14 @@
dependencies:
"@types/node" "*"

"@types/prompts@^2.4.9":
version "2.4.9"
resolved "https://registry.yarnpkg.com/@types/prompts/-/prompts-2.4.9.tgz#8775a31e40ad227af511aa0d7f19a044ccbd371e"
integrity sha512-qTxFi6Buiu8+50/+3DGIWLHM6QuWsEKugJnnP6iv2Mc4ncxE4A/OJkjuVOA+5X0X1S/nq5VJRa8Lu+nwcvbrKA==
dependencies:
"@types/node" "*"
kleur "^3.0.3"

"@types/prop-types@*":
version "15.7.4"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.4.tgz#fcf7205c25dff795ee79af1e30da2c9790808f11"
Expand Down Expand Up @@ -4344,6 +4352,13 @@ ansi-align@^3.0.0:
dependencies:
string-width "^3.0.0"

ansi-align@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/ansi-align/-/ansi-align-3.0.1.tgz#0cdf12e111ace773a86e9a1fad1225c43cb19a59"
integrity sha512-IOfwwBF5iczOjp/WeY4YxyjqAFMQoZufdQWDd19SEExbVLNXqvpzSJ/M7Za4/sCPmQ0+GRquoA7bGcINcxew6w==
dependencies:
string-width "^4.1.0"

ansi-colors@^3.0.0:
version "3.2.4"
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-3.2.4.tgz#e3a3da4bfbae6c86a9c285625de124a234026fbf"
Expand Down Expand Up @@ -4396,6 +4411,11 @@ ansi-regex@^5.0.0, ansi-regex@^5.0.1:
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304"
integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==

ansi-regex@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.0.1.tgz#3183e38fae9a65d7cb5e53945cd5897d0260a06a"
integrity sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==

ansi-styles@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe"
Expand All @@ -4420,6 +4440,11 @@ ansi-styles@^5.0.0:
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-5.2.0.tgz#07449690ad45777d1924ac2abb2fc8895dba836b"
integrity sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==

ansi-styles@^6.1.0:
version "6.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.2.1.tgz#0e62320cf99c21afff3b3012192546aacbfb05c5"
integrity sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==

ansi-to-html@^0.6.11:
version "0.6.15"
resolved "https://registry.yarnpkg.com/ansi-to-html/-/ansi-to-html-0.6.15.tgz#ac6ad4798a00f6aa045535d7f6a9cb9294eebea7"
Expand Down Expand Up @@ -5120,6 +5145,20 @@ boxen@^5.1.2:
widest-line "^3.1.0"
wrap-ansi "^7.0.0"

boxen@^7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/boxen/-/boxen-7.1.1.tgz#f9ba525413c2fec9cdb88987d835c4f7cad9c8f4"
integrity sha512-2hCgjEmP8YLWQ130n2FerGv7rYpfBmnmp9Uy2Le1vge6X3gZIfSmEzP5QTDElFxcvVcXlEn8Aq6MU/PZygIOog==
dependencies:
ansi-align "^3.0.1"
camelcase "^7.0.1"
chalk "^5.2.0"
cli-boxes "^3.0.0"
string-width "^5.1.2"
type-fest "^2.13.0"
widest-line "^4.0.1"
wrap-ansi "^8.1.0"

bplist-parser@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/bplist-parser/-/bplist-parser-0.1.1.tgz#d60d5dcc20cba6dc7e1f299b35d3e1f95dafbae6"
Expand Down Expand Up @@ -5465,6 +5504,11 @@ camelcase@^6.2.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.2.0.tgz#924af881c9d525ac9d87f40d964e5cea982a1809"
integrity sha512-c7wVvbw3f37nuobQNtgsgG9POC9qMbNuMQmTCqZv23b6MIz0fcYpBiOlv9gEN/hdLdnZTDQhg6e9Dq5M1vKvfg==

camelcase@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-7.0.1.tgz#f02e50af9fd7782bc8b88a3558c32fd3a388f048"
integrity sha512-xlx1yCK2Oc1APsPXDL2LdlNP6+uu8OCDdhOBSVT279M/S+y75O30C2VuD8T2ogdePBBl7PfPF4504tnLgX3zfw==

caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001254:
version "1.0.30001259"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001259.tgz#ae21691d3da9c4be6144403ac40f71d9f6efd790"
Expand Down Expand Up @@ -5536,6 +5580,11 @@ chalk@^4.0.0, chalk@^4.1.0, chalk@^4.1.1, chalk@^4.1.2:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chalk@^5.2.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.3.0.tgz#67c20a7ebef70e7f3970a01f90fa210cb6860385"
integrity sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==

character-entities-legacy@^1.0.0:
version "1.1.4"
resolved "https://registry.yarnpkg.com/character-entities-legacy/-/character-entities-legacy-1.1.4.tgz#94bc1845dce70a5bb9d2ecc748725661293d8fc1"
Expand Down Expand Up @@ -5667,6 +5716,11 @@ cli-boxes@^2.2.1:
resolved "https://registry.yarnpkg.com/cli-boxes/-/cli-boxes-2.2.1.tgz#ddd5035d25094fce220e9cab40a45840a440318f"
integrity sha512-y4coMcylgSCdVinjiDBuR8PCC2bLjyGTwEmPb9NHR/QaNU6EUOXcTY/s6VjGMD6ENSEaeQYHCY0GNGS5jfMwPw==

cli-boxes@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/cli-boxes/-/cli-boxes-3.0.0.tgz#71a10c716feeba005e4504f36329ef0b17cf3145"
integrity sha512-/lzGpEWL/8PfI0BmBOPRwp0c/wFNX1RdUML3jK/RcSBA9T8mZDdQpqYBKtCFTOfQbwPqWEOpjqW+Fnayc0969g==

cli-cursor@^2.0.0, cli-cursor@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-2.1.0.tgz#b35dac376479facc3e94747d41d0d0f5238ffcb5"
Expand Down Expand Up @@ -6618,6 +6672,11 @@ duplexify@^3.4.2, duplexify@^3.6.0:
readable-stream "^2.0.0"
stream-shift "^1.0.0"

eastasianwidth@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==

ee-first@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
Expand Down Expand Up @@ -6656,7 +6715,7 @@ emoji-regex@^8.0.0:
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
integrity sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==

emoji-regex@^9.0.0:
emoji-regex@^9.0.0, emoji-regex@^9.2.2:
version "9.2.2"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-9.2.2.tgz#840c8803b0d8047f4ff0cf963176b32d4ef3ed72"
integrity sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==
Expand Down Expand Up @@ -12271,6 +12330,14 @@ prompts@^2.4.0:
kleur "^3.0.3"
sisteransi "^1.0.5"

prompts@^2.4.2:
version "2.4.2"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"
integrity sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q==
dependencies:
kleur "^3.0.3"
sisteransi "^1.0.5"

prop-types@^15.0.0, prop-types@^15.8.1:
version "15.8.1"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
Expand Down Expand Up @@ -14040,6 +14107,15 @@ string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0:
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.0"

string-width@^5.0.1, string-width@^5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-5.1.2.tgz#14f8daec6d81e7221d2a357e668cab73bdbca794"
integrity sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA==
dependencies:
eastasianwidth "^0.2.0"
emoji-regex "^9.2.2"
strip-ansi "^7.0.1"

"string.prototype.matchall@^4.0.0 || ^3.0.1", string.prototype.matchall@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/string.prototype.matchall/-/string.prototype.matchall-4.0.5.tgz#59370644e1db7e4c0c045277690cf7b01203c4da"
Expand Down Expand Up @@ -14188,6 +14264,13 @@ strip-ansi@^6.0.1:
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
integrity sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==
dependencies:
ansi-regex "^6.0.1"

strip-bom@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-2.0.0.tgz#6219a85616520491f35788bdbf1447a99c7e6b0e"
Expand Down Expand Up @@ -14881,7 +14964,7 @@ type-fest@^0.8.1:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==

type-fest@^2.19.0:
type-fest@^2.13.0, type-fest@^2.19.0:
version "2.19.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.19.0.tgz#88068015bb33036a598b952e55e9311a60fd3a9b"
integrity sha512-RAH822pAdBgcNMAfWnCBU3CFZcfZ/i1eZjwFU/dsLKumyuuP3niueg2UAukXYF0E2AAoc82ZSSf9J0WQBinzHA==
Expand Down Expand Up @@ -15827,6 +15910,13 @@ widest-line@^3.1.0:
dependencies:
string-width "^4.0.0"

widest-line@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/widest-line/-/widest-line-4.0.1.tgz#a0fc673aaba1ea6f0a0d35b3c2795c9a9cc2ebf2"
integrity sha512-o0cyEG0e8GPzT4iGHphIOh0cJOV8fivsXxddQasHPHfoZf1ZexrfeA21w2NaEN1RHE+fXlfISmOE8R9N3u3Qig==
dependencies:
string-width "^5.0.1"

wildcard@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/wildcard/-/wildcard-1.1.2.tgz#a7020453084d8cd2efe70ba9d3696263de1710a5"
Expand Down Expand Up @@ -15890,6 +15980,15 @@ wrap-ansi@^7.0.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"
integrity sha512-si7QWI6zUMq56bESFvagtmzMdGOtoxfR+Sez11Mobfc7tm+VkUckk9bW2UeffTGVUbOksxmSw0AA2gs8g71NCQ==
dependencies:
ansi-styles "^6.1.0"
string-width "^5.0.1"
strip-ansi "^7.0.1"

wrapped@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/wrapped/-/wrapped-1.0.1.tgz#c783d9d807b273e9b01e851680a938c87c907242"
Expand Down

0 comments on commit 0e059fa

Please sign in to comment.