From 2c26b04de82789c750cc5e3960c64db4c26c3b36 Mon Sep 17 00:00:00 2001 From: Miki Date: Mon, 19 Jun 2023 14:32:48 -0700 Subject: [PATCH] Enable local consumption of OUI (#813) * Fix linting sass files With the upgrade os `eslint`, the location of the default formatter changed. This is just a hack to work around the problem in the now abandoned `sass-lint`. Signed-off-by: Miki * Add preinstall script Signed-off-by: Miki --------- Signed-off-by: Miki --- .npmignore | 4 +- .sass-lint.yml | 2 + package.json | 1 + scripts/preinstall.js | 125 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 scripts/preinstall.js diff --git a/.npmignore b/.npmignore index fd522a52f4..f136ce1725 100644 --- a/.npmignore +++ b/.npmignore @@ -13,8 +13,8 @@ packages/eslint-plugin # typescript output types/ -# ignore everything in `scripts` except postinstall.js -scripts/!(postinstall.js) +# ignore everything in `scripts` except postinstall.js and preinstall.js +scripts/!(postinstall.js|preinstall.js) src/**/*.!(scss) diff --git a/.sass-lint.yml b/.sass-lint.yml index 794d2368de..f2031b4e4f 100644 --- a/.sass-lint.yml +++ b/.sass-lint.yml @@ -1,3 +1,5 @@ +options: + formatter: '../cli-engine/formatters/stylish' files: include: - 'src/**/*.s+(a|c)ss' diff --git a/package.json b/package.json index 92aec68da5..84454a4dbb 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "yo-doc": "yo ./generator-oui/app/documentation.js", "release": "node ./scripts/release.js", "postinstall": "node ./scripts/postinstall.js", + "preinstall": "node ./scripts/preinstall.js", "version": "node ./scripts/update-changelog-version.js" }, "repository": { diff --git a/scripts/preinstall.js b/scripts/preinstall.js new file mode 100644 index 0000000000..94031f9ca4 --- /dev/null +++ b/scripts/preinstall.js @@ -0,0 +1,125 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +const { rmSync, readdirSync } = require('fs'); +const { join } = require('path'); + +const { INIT_CWD, PWD = process.cwd() } = process.env; + +// Only run when installed as a dep +if (!INIT_CWD?.startsWith?.(PWD)) { + /* These are deps and types which get installed when a production package is installed. + * When this library is linked as a dep to another project, having all the deps could + * confuse or conflict the project's compilers. + * + * When being installed as dep of another project in production, `node_modules` would + * be empty. + */ + const depsToKeep = [ + '@types', + 'csstype', + 'is-buffer', + 'is-plain-obj', + 'mdast-util-definitions', + 'mdast-util-to-hast', + 'mdurl', + 'react-focus-lock', + 'react-focus-on', + 'react-is', + 'react-remove-scroll', + 'react-remove-scroll-bar', + 'react-style-singleton', + 'rehype-react', + 'remark-parse', + 'remark-rehype', + 'unified', + 'unist-builder', + 'unist-util-generated', + 'unist-util-is', + 'unist-util-position', + 'unist-util-remove-position', + 'unist-util-stringify-position', + 'unist-util-visit', + 'unist-util-visit-parents', + 'use-callback-ref', + 'use-sidecar', + 'uuid', + 'vfile-message', + ]; + + for (const name of readdirSync('node_modules')) { + if (!depsToKeep.includes(name)) rmSync(join('node_modules', name), { recursive: true, force: true }); + } + + const typesToKeep = [ + 'chroma-js', + 'lodash', + 'mdast', + 'numeral', + 'prismjs', + 'prop-types', + 'react', + 'react-beautiful-dnd', + 'react-dom', + 'react-input-autosize', + 'react-virtualized-auto-sizer', + 'react-window', + 'refractor', + 'resize-observer-browser', + 'scheduler', + 'unist', + 'vfile-message', + ]; + + for (const name of readdirSync('node_modules/@types')) { + if (!typesToKeep.includes(name)) rmSync(join('node_modules/@types', name), { recursive: true, force: true }); + } + + const toDeleteFromRoot = [ + '.DS_Store', + '.cache-loader', + '.eslintcache', + '.git', + '.idea', + '.nvmrc', + '.vscode', + 'docs', + 'generator-oui', + 'packages/eslint-plugin', + 'packages/react-datepicker', + 'reports', + 'src-docs', + 'test', + 'tmp', + 'tsconfig-builttypes.json', + 'tsconfig.json', + 'types', + 'wiki', + 'yarn-error.log', + 'yarn.lock', + ]; + + for (const name of toDeleteFromRoot) { + rmSync(name, { recursive: true, force: true }); + } + + const scriptsToKeep = [ + 'postinstall.js', + 'preinstall.js' + ]; + + for (const name of readdirSync('scripts')) { + if (!scriptsToKeep.includes(name)) rmSync(join('scripts', name), { recursive: true, force: true }); + } + + const deleteNonSCSS = (loc) => { + for (const entry of readdirSync(loc, { withFileTypes: true })) { + if (entry.isDirectory()) deleteNonSCSS(join(loc, entry.name)); + else if (entry.isFile() && !entry.name.endsWith('.scss')) rmSync(join(loc, entry.name), { force: true }); + } + }; + + deleteNonSCSS('src'); +}