From ca85241db2fc36d3057b0f18a78ddf9079936def Mon Sep 17 00:00:00 2001 From: Garrett Stevens Date: Tue, 21 Dec 2021 13:22:16 -0700 Subject: [PATCH] Use setup script from devtools instead of locally --- package.json | 4 +- scripts/setup.ts | 200 ------------------------------------------ scripts/tsconfig.json | 10 --- 3 files changed, 2 insertions(+), 212 deletions(-) delete mode 100644 scripts/setup.ts delete mode 100644 scripts/tsconfig.json diff --git a/package.json b/package.json index fdda28f..7e0b2dd 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ }, "scripts": { "setup": "npm-run-all setup:*", - "setup:file": "ts-node scripts/setup.ts", + "setup:file": "jbrowse-plugin-setup", "setup:jbrowse": "npm-run-all setup:jbrowse:*", "setup:jbrowse:clean": "rimraf .jbrowse", "setup:jbrowse:create": "jbrowse create .jbrowse", @@ -67,7 +67,7 @@ "@babel/preset-react": "^7.10.4", "@jbrowse/cli": "^1.5.1", "@jbrowse/core": "^1.5.2", - "@jbrowse/development-tools": "^1.5.3", + "@jbrowse/development-tools": "^1.5.4", "@material-ui/core": "^4.9.13", "@material-ui/lab": "^4.0.0-alpha.45", "@schemastore/package": "^0.0.6", diff --git a/scripts/setup.ts b/scripts/setup.ts deleted file mode 100644 index 213208d..0000000 --- a/scripts/setup.ts +++ /dev/null @@ -1,200 +0,0 @@ -import fs from 'fs' -import os from 'os' -import path from 'path' -import chalk from 'chalk' -import { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package' - -const fsPromises = fs.promises - -type JSONValue = - | string - | number - | boolean - | null - | JSONValue[] - | { [key: string]: JSONValue } - -async function main() { - const packageJSON = await readJSON(path.join(__dirname, '..', 'package.json')) - const rawPackageName = packageJSON.name - // ensure that yarn init has been run - if (rawPackageName === undefined) { - console.warn( - chalk.red( - 'No name defined in package.json. Please run "yarn init" (or "npm init") before running "yarn setup" (or "npm run setup").', - ), - ) - process.exit(1) - } - if (rawPackageName === 'jbrowse-plugin-template') { - console.warn( - chalk.red( - 'Please run "yarn init" (or "npm init") before running "yarn setup" (or "npm run setup").', - ), - ) - process.exit(1) - } - - const packageName = getSafePackageName(rawPackageName) - const prefix = 'jbrowse-plugin-' - const pluginClassName = toPascalCase( - packageName.startsWith(prefix) - ? packageName.slice(prefix.length) - : packageName, - ) - - updatePackageJSON(pluginClassName, packageJSON) - updateSrcIndex(pluginClassName) - updateJBrowseConfig(packageName, pluginClassName) - updateExampleFixture(packageName, pluginClassName) - updateReadme(rawPackageName, packageJSON.repository) -} - -function updatePackageJSON( - pluginName: string, - packageJSON: JSONSchemaForNPMPackageJsonFiles, -) { - // 1. Change "name" in the "jbrowse-plugin" and "config" fields to the name of your project (e.g. "MyProject") - packageJSON['jbrowse-plugin'].name = pluginName - if (!packageJSON.config) { - packageJSON.config = {} - } - packageJSON.config.jbrowse.plugin.name = pluginName - - // this overwrites package.json - writeJSON('package.json', packageJSON) -} - -// replace default plugin name in example plugin class -async function updateSrcIndex(pluginClassName: string) { - const indexFilePath = path.join('src', 'index.ts') - let indexFile = await fsPromises.readFile(indexFilePath, 'utf-8') - indexFile = indexFile.replace(/TemplatePlugin/g, `${pluginClassName}Plugin`) - fsPromises.writeFile(indexFilePath, indexFile) -} - -// replace default plugin name and url with project name and dist file -async function updateJBrowseConfig(packageName: string, pluginName: string) { - const jbrowseConfig = await readJSON( - path.join(__dirname, '..', 'jbrowse_config.json'), - ) - jbrowseConfig.plugins[0].name = pluginName - jbrowseConfig.plugins[0].url = `http://localhost:9000/dist/${packageName}.umd.development.js` - writeJSON('jbrowse_config.json', jbrowseConfig) -} - -// replace default plugin name and url with project name and dist file -async function updateExampleFixture(packageName: string, pluginName: string) { - const fixtureLocation = path.join( - __dirname, - '..', - 'cypress', - 'fixtures', - 'hello_view.json', - ) - const exampleFixture = await readJSON(fixtureLocation) - exampleFixture.plugins[0].name = pluginName - exampleFixture.plugins[0].url = `http://localhost:9000/dist/${packageName}.umd.development.js` - writeJSON(fixtureLocation, exampleFixture) -} - -async function updateReadme( - packageName: string, - repository: JSONSchemaForNPMPackageJsonFiles['repository'], -) { - // add status badge to README - const repoUrl = getUrlFromRepo(repository) - let readmeLines = (await fsPromises.readFile('README.md', 'utf-8')).split( - /\r?\n/, - ) - if (readmeLines[0].startsWith(`# ${packageName}`)) { - return - } - readmeLines[0] = `# ${packageName}` - if (repoUrl !== undefined) { - readmeLines.unshift( - `![Integration](${repoUrl}/workflows/Integration/badge.svg?branch=main)${os.EOL}`, - ) - } - fsPromises.writeFile('README.md', readmeLines.join(os.EOL), 'utf8') -} - -/* -**************************** -Helpers -**************************** -*/ - -async function writeJSON(path: string, data: JSONValue) { - let jsonString - try { - jsonString = JSON.stringify(data, null, 2) - } catch (error) { - console.error('There was a problem converting an object to JSON') - throw error - } - return fsPromises.writeFile(path, `${jsonString}\n`) -} - -async function readJSON(path: string) { - let jsonString - try { - jsonString = await fsPromises.readFile(path, 'utf8') - } catch (error) { - console.error(`Could not read JSON file at ${path}`) - throw error - } - let jsonData - try { - jsonData = JSON.parse(jsonString) - } catch (error) { - console.error( - `Could not parse JSON file at ${path}, check for JSON syntax errors`, - ) - throw error - } - return jsonData -} - -// snagged from https://stackoverflow.com/a/53952925 -function toPascalCase(string: string) { - return `${string}` - .replace(new RegExp(/[-_]+/, 'g'), ' ') - .replace(new RegExp(/[^\w\s]/, 'g'), '') - .replace( - new RegExp(/\s+(.)(\w+)/, 'g'), - ($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`, - ) - .replace(new RegExp(/\s/, 'g'), '') - .replace(new RegExp(/\w/), s => s.toUpperCase()) -} - -function getSafePackageName(name: string) { - return name - .toLowerCase() - .replace(/(^@.*\/)|((^[^a-zA-Z]+)|[^\w.-])|([^a-zA-Z0-9]+$)/g, '') -} - -function getUrlFromRepo(repo: JSONSchemaForNPMPackageJsonFiles['repository']) { - if (repo === undefined) { - return repo - } - let url = undefined - if (typeof repo === 'string') { - url = repo - } else if (typeof repo === 'object') { - url = repo.url - } - - if (typeof url === 'string') { - if (url.includes('github.com')) { - return url.replace(/\.git$/, '') - } - if (url.startsWith('github:')) { - return `https://github.com/${url.split(':')[1]}` - } - } - return undefined -} - -main() diff --git a/scripts/tsconfig.json b/scripts/tsconfig.json deleted file mode 100644 index bf43d53..0000000 --- a/scripts/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "../tsconfig", - "compilerOptions": { - "rootDir": ".", - "lib": ["ES2019"], - "module": "commonjs", - "target": "ES2019", - }, - "include": ["."], -}