From 05a452a1aca7b1afc299caf2316eeeb580d09a5b Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Fri, 6 Aug 2021 23:04:59 -0700 Subject: [PATCH 1/6] feat: also allow nonscoped packages --- packages/plugin-tools/migrations.json | 9 + packages/plugin-tools/package.json | 2 +- .../add-angular/files/index.ts__tmpl__ | 2 +- .../generators/add-angular/files/package.json | 4 +- .../add-angular/files/tsconfig.angular.json | 2 +- .../src/generators/add-angular/index.ts | 10 +- .../generators/add-package/files/README.md | 4 +- .../generators/add-package/files/package.json | 2 +- .../src/generators/add-package/index.ts | 12 +- .../src/generators/add-package/schema.d.ts | 1 + .../src/generators/add-package/schema.json | 6 + .../src/generators/config/schema.json | 2 +- .../src/generators/focus-packages/index.ts | 13 +- .../src/generators/remove-package/index.ts | 48 +- .../__name__.component.ts__tmpl__ | 2 +- .../__name__/index.ts__tmpl__ | 2 +- .../files_react/__name__.tsx__tmpl__ | 2 +- .../files_svelte/__name__.svelte__tmpl__ | 2 +- .../files_vue/__name__.vue__tmpl__ | 2 +- .../files_xml/__name__.ts__tmpl__ | 2 +- .../sync-packages-with-demos/index.ts | 6 +- .../migrations/update-2-0-0/update-2-0-0.ts | 483 ++++++++---------- .../update-2-1-0/_files/build-finish | 57 +++ .../migrations/update-2-1-0/update-2-1-0.ts | 20 + packages/plugin-tools/src/utils/demo.ts | 10 +- packages/plugin-tools/src/utils/workspace.ts | 39 +- 26 files changed, 397 insertions(+), 347 deletions(-) create mode 100644 packages/plugin-tools/src/migrations/update-2-1-0/_files/build-finish create mode 100644 packages/plugin-tools/src/migrations/update-2-1-0/update-2-1-0.ts diff --git a/packages/plugin-tools/migrations.json b/packages/plugin-tools/migrations.json index f1e1966..118f6d1 100644 --- a/packages/plugin-tools/migrations.json +++ b/packages/plugin-tools/migrations.json @@ -1,19 +1,28 @@ { "schematics": { "update-to-1.0.6": { + "cli": "nx", "version": "1.0.6", "description": "Migrate tools to 1.0.6", "factory": "./src/migrations/update-1-0-6/update-1-0-6" }, "update-to-1.1.0": { + "cli": "nx", "version": "1.1.0", "description": "Migrate tools to 1.1.0", "factory": "./src/migrations/update-1-1-0/update-1-1-0" }, "update-to-2.0.0": { + "cli": "nx", "version": "2.0.0", "description": "Migrate tools to 2.0.0", "factory": "./src/migrations/update-2-0-0/update-2-0-0" + }, + "update-to-2.1.0": { + "cli": "nx", + "version": "2.1.0", + "description": "Migrate tools to 2.1.0", + "factory": "./src/migrations/update-2-1-0/update-2-1-0" } } } diff --git a/packages/plugin-tools/package.json b/packages/plugin-tools/package.json index 6b9c671..89c2479 100644 --- a/packages/plugin-tools/package.json +++ b/packages/plugin-tools/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript/plugin-tools", - "version": "2.0.7", + "version": "2.1.0-rc.0", "description": "NativeScript plugin tooling for workspaces", "homepage": "https://nativescript.org", "repository": { diff --git a/packages/plugin-tools/src/generators/add-angular/files/index.ts__tmpl__ b/packages/plugin-tools/src/generators/add-angular/files/index.ts__tmpl__ index c9063af..df9ea8a 100644 --- a/packages/plugin-tools/src/generators/add-angular/files/index.ts__tmpl__ +++ b/packages/plugin-tools/src/generators/add-angular/files/index.ts__tmpl__ @@ -1,6 +1,6 @@ import { NgModule } from '@angular/core'; import { registerElement } from '@nativescript/angular'; -import { <%= stringUtils.classify(name) %> } from '<%= npmScope %>/<%= name %>'; +import { <%= stringUtils.classify(name) %> } from '<%= npmPackageName %>'; @NgModule() export class NativeScript<%= stringUtils.classify(name) %>Module {} diff --git a/packages/plugin-tools/src/generators/add-angular/files/package.json b/packages/plugin-tools/src/generators/add-angular/files/package.json index ca7957c..380241f 100644 --- a/packages/plugin-tools/src/generators/add-angular/files/package.json +++ b/packages/plugin-tools/src/generators/add-angular/files/package.json @@ -1,12 +1,12 @@ { - "name": "<%= nxNpmScope %>-<%= name %>-angular", + "name": "<%= npmPackageName %>-angular", "ngPackage": { "lib": { "entryFile": "index.ts", "umdModuleIds": { "@nativescript/core": "ns-core", "@nativescript/angular": "ns-angular", - "<%= npmScope %>/<%= name %>": "ns-<%= name %>" + "<%= npmPackageName %>": "ns-<%= name %>" } }, "allowedNonPeerDependencies": [ diff --git a/packages/plugin-tools/src/generators/add-angular/files/tsconfig.angular.json b/packages/plugin-tools/src/generators/add-angular/files/tsconfig.angular.json index a3c8a0f..64fa5d3 100644 --- a/packages/plugin-tools/src/generators/add-angular/files/tsconfig.angular.json +++ b/packages/plugin-tools/src/generators/add-angular/files/tsconfig.angular.json @@ -4,7 +4,7 @@ "types": ["node"], "baseUrl": ".", "paths": { - "<%= npmScope %>/<%= name %>": ["../../../dist/packages/<%= name %>"] + "<%= npmPackageName %>": ["../../../dist/packages/<%= name %>"] } } } diff --git a/packages/plugin-tools/src/generators/add-angular/index.ts b/packages/plugin-tools/src/generators/add-angular/index.ts index ba141bd..52e5aa0 100644 --- a/packages/plugin-tools/src/generators/add-angular/index.ts +++ b/packages/plugin-tools/src/generators/add-angular/index.ts @@ -1,19 +1,23 @@ -import { generateFiles, joinPathFragments, Tree } from '@nrwl/devkit'; +import { generateFiles, joinPathFragments, readJson, Tree } from '@nrwl/devkit'; import { stringUtils } from '@nrwl/workspace'; import { prerun, getNpmScope, getNxNpmScope } from '../../utils'; import { Schema } from './schema'; let name: string; +let npmPackageName: string; export default async function (tree: Tree, schema: Schema) { name = stringUtils.dasherize(schema.name.toLowerCase()); prerun(tree); addAngularFiles(tree); - console.log(`Angular support added for "${getNpmScope()}/${name}". Continue developing the Angular support in the packages/${name}/angular folder.`); + console.log(`Angular support added for "${npmPackageName}". Continue developing the Angular support in the packages/${name}/angular folder.`); } function addAngularFiles(tree: Tree) { console.log(`Adding Angular support to ${name}...`); - generateFiles(tree, joinPathFragments(__dirname, 'files'), `./packages/${name}/angular`, { name, npmScope: getNpmScope(), nxNpmScope: getNxNpmScope(), stringUtils, tmpl: '', dot: '.' }); + const packageJson = readJson(tree, `packages/${name}/package.json`); + npmPackageName = packageJson.name; + + generateFiles(tree, joinPathFragments(__dirname, 'files'), `./packages/${name}/angular`, { name, npmPackageName, npmScope: getNpmScope(), nxNpmScope: getNxNpmScope(), stringUtils, tmpl: '', dot: '.' }); } diff --git a/packages/plugin-tools/src/generators/add-package/files/README.md b/packages/plugin-tools/src/generators/add-package/files/README.md index 2f5fa7d..51444fc 100644 --- a/packages/plugin-tools/src/generators/add-package/files/README.md +++ b/packages/plugin-tools/src/generators/add-package/files/README.md @@ -1,7 +1,7 @@ -# <%= npmScope %>/<%= name %> +# <%= npmPackageName %> ```javascript -ns plugin add <%= npmScope %>/<%= name %> +ns plugin add <%= npmPackageName %> ``` ## Usage diff --git a/packages/plugin-tools/src/generators/add-package/files/package.json b/packages/plugin-tools/src/generators/add-package/files/package.json index 76d8f62..0ecfc08 100644 --- a/packages/plugin-tools/src/generators/add-package/files/package.json +++ b/packages/plugin-tools/src/generators/add-package/files/package.json @@ -1,5 +1,5 @@ { - "name": "<%= npmScope %>/<%= name %>", + "name": "<%= npmPackageName %>", "version": "1.0.0", "description": "Add a plugin description", "main": "index", diff --git a/packages/plugin-tools/src/generators/add-package/index.ts b/packages/plugin-tools/src/generators/add-package/index.ts index a116453..bd98af4 100644 --- a/packages/plugin-tools/src/generators/add-package/index.ts +++ b/packages/plugin-tools/src/generators/add-package/index.ts @@ -5,8 +5,10 @@ import syncPackagesWithDemos from '../sync-packages-with-demos'; import { Schema } from './schema'; let name: string; +let npmPackageName: string; export default async function (tree: Tree, schema: Schema) { name = stringUtils.dasherize(schema.name); + npmPackageName = schema.isScoped ? `${getNpmScope()}/${name}` : name; prerun(tree); addPackageFiles(tree); addProjectToNxJsonInTree(name, {}); @@ -22,11 +24,11 @@ export default async function (tree: Tree, schema: Schema) { true ); - console.log(`"${getNpmScope()}/${name}" created and added to all demo apps. Ready to develop!`); + console.log(`"${npmPackageName}" created and added to all demo apps. Ready to develop!`); } function addPackageFiles(tree: Tree) { - generateFiles(tree, joinPathFragments(__dirname, 'files'), `./packages/${name}`, { name, npmScope: getNpmScope(), stringUtils, tmpl: '', dot: '.' }); + generateFiles(tree, joinPathFragments(__dirname, 'files'), `./packages/${name}`, { name, npmPackageName, npmScope: getNpmScope(), stringUtils, tmpl: '', dot: '.' }); } function updateWorkspaceConfig(tree: Tree) { @@ -103,11 +105,11 @@ function updateWorkspaceScripts(tree: Tree) { const buildSectionIndex = workspaceScripts.indexOf(`'build-all':`); const buildStart = workspaceScripts.substring(0, buildSectionIndex); const buildEnd = workspaceScripts.substring(buildSectionIndex, workspaceScripts.length); - const newBuild = `// ${getNpmScope()}/${name} + const newBuild = `// ${npmPackageName} '${name}': { build: { script: 'nx run ${name}:build.all', - description: '${getNpmScope()}/${name}: Build', + description: '${npmPackageName}: Build', }, }, `; @@ -119,7 +121,7 @@ function updateWorkspaceScripts(tree: Tree) { const focusEnd = workspaceScripts.substring(focusSectionIndex, workspaceScripts.length); const newFocus = `'${name}': { script: 'nx run ${name}:focus', - description: 'Focus on ${getNpmScope()}/${name}', + description: 'Focus on ${npmPackageName}', }, `; workspaceScripts = `${focusStart}${newFocus} ${focusEnd}`; diff --git a/packages/plugin-tools/src/generators/add-package/schema.d.ts b/packages/plugin-tools/src/generators/add-package/schema.d.ts index 995dbae..e300faa 100644 --- a/packages/plugin-tools/src/generators/add-package/schema.d.ts +++ b/packages/plugin-tools/src/generators/add-package/schema.d.ts @@ -1,3 +1,4 @@ export interface Schema { name: string; + isScoped?: boolean; } diff --git a/packages/plugin-tools/src/generators/add-package/schema.json b/packages/plugin-tools/src/generators/add-package/schema.json index 78bc8e0..b80b28f 100644 --- a/packages/plugin-tools/src/generators/add-package/schema.json +++ b/packages/plugin-tools/src/generators/add-package/schema.json @@ -13,6 +13,12 @@ "index": 0 }, "x-prompt": "What should the new package be named?" + }, + "isScoped": { + "type": "boolean", + "default": true, + "description": "Whether the new package should be scoped to the npm scope of the workspace or not.", + "x-prompt": "Should it use the npm scope of the workspace? 'Y' is the most common. This is the difference between, for example @nativescript/camera vs. nativescript-camera. If you choose 'N' here, be sure you have specified the full npm name you would like to use during publishing." } }, "required": ["name"] diff --git a/packages/plugin-tools/src/generators/config/schema.json b/packages/plugin-tools/src/generators/config/schema.json index 6734e24..928e8d2 100644 --- a/packages/plugin-tools/src/generators/config/schema.json +++ b/packages/plugin-tools/src/generators/config/schema.json @@ -12,7 +12,7 @@ "$source": "argv", "index": 0 }, - "x-prompt": "What npm scope would you like to use for this workspace?" + "x-prompt": "What npm scope would you like to use for this workspace? This will become the default npm scope of the workspace however you can add packages that are non-scoped at anytime via 'npm run add'" } } } diff --git a/packages/plugin-tools/src/generators/focus-packages/index.ts b/packages/plugin-tools/src/generators/focus-packages/index.ts index a1715b0..3fcb8b3 100644 --- a/packages/plugin-tools/src/generators/focus-packages/index.ts +++ b/packages/plugin-tools/src/generators/focus-packages/index.ts @@ -1,4 +1,4 @@ -import { sanitizeCollectionArgs, getDemoTypeFromName, updateDemoDependencies, setPackageNamesToUpdate, getAllPackages, resetIndexForDemoType, getPluginDemoPath, updateDemoSharedIndex, getNpmScope, prerun } from '../../utils'; +import { sanitizeCollectionArgs, getDemoTypeFromName, updateDemoDependencies, setPackageNamesToUpdate, getAllPackages, resetIndexForDemoType, getPluginDemoPath, updateDemoSharedIndex, getNpmScope, prerun, getNpmPackageNames } from '../../utils'; import { Schema } from './schema'; import { wrapAngularDevkitSchematic } from '@nrwl/devkit/ngcli-adapter'; import { Tree } from '@nrwl/devkit'; @@ -14,17 +14,16 @@ export default async function (tree: Tree, schema: Schema) { await nstudioFocus(tree, { name: schema.name, }); + setPackageNamesToUpdate(focusPackages); + allPackages = getAllPackages(tree); + const npmPackageNames = getNpmPackageNames(); + // console.log('allPackages:', allPackages); // Isolate code in demo apps by default based on focus if (!schema.ignoreDemos) { - setPackageNamesToUpdate(focusPackages); - allPackages = getAllPackages(tree); - // console.log('allPackages:', allPackages); - // adjust demo shared index for focusing updateDemoSharedIndex(tree, allPackages, focusPackages); // apps - const appFolders = tree.children('apps'); for (const dir of appFolders) { // console.log(`demoAppRoot: ${demoAppRoot}`); @@ -72,7 +71,7 @@ export default async function (tree: Tree, schema: Schema) { } const isFocusing = focusPackages && focusPackages.length > 0; - const focusTargets = (focusPackages && focusPackages.length ? focusPackages : allPackages).map((n) => `\n${getNpmScope()}/${n}`).join(''); + const focusTargets = (focusPackages && focusPackages.length ? focusPackages : allPackages).map((n) => `\n${npmPackageNames[n]}`).join(''); console.log(`${isFocusing ? 'Focusing workspace on:' : 'Resetting workspace for:'}\n${focusTargets}\n\n`); if (!schema.ignoreDemos) { console.log(` > NOTE: Clean the demo app you plan to test with before running now that the demo code has been updated.\n`); diff --git a/packages/plugin-tools/src/generators/remove-package/index.ts b/packages/plugin-tools/src/generators/remove-package/index.ts index 028fa0d..cb8a21f 100644 --- a/packages/plugin-tools/src/generators/remove-package/index.ts +++ b/packages/plugin-tools/src/generators/remove-package/index.ts @@ -1,31 +1,39 @@ -import { Tree, removeProjectConfiguration, updateJson, readProjectConfiguration, updateProjectConfiguration } from '@nrwl/devkit'; +import { Tree, removeProjectConfiguration, updateJson, readProjectConfiguration, updateProjectConfiguration, readJson } from '@nrwl/devkit'; import { stringUtils } from '@nrwl/workspace'; -import { prerun, getNpmScope, getDemoTypes, getDemoAppRoot, getPluginDemoPath, SupportedDemoType, checkPackages, getDemoIndexPathForType, getPackageNamesToUpdate, getPathToPackageForDemo, updateReadMe } from '../../utils'; +import { prerun, getNpmScope, getDemoTypes, getDemoAppRoot, getPluginDemoPath, SupportedDemoType, checkPackages, getDemoIndexPathForType, getPackageNamesToUpdate, getPathToPackageForDemo, updateReadMe, getNpmPackageNames } from '../../utils'; import { Schema } from './schema'; let name: string; +let npmPackageName: string; export default function (tree: Tree, schema: Schema) { name = stringUtils.dasherize(schema.name); prerun(tree); - removePackage(tree); - - removeProjectConfiguration(tree, name); - removeFromBuildAll(tree); - - updateReadMe(tree, getUpdatedPackages(tree)); - - for (const t of getDemoTypes()) { - const demoAppRoot = getDemoAppRoot(t); - removeDemoFiles(tree, t, demoAppRoot); - removeFromDemoIndex(tree, t, demoAppRoot); - updateDemoDependencies(tree, demoAppRoot); + const packagePath = `packages/${name}/package.json`; + if (tree.exists(packagePath)) { + const packageJson = readJson(tree, packagePath); + npmPackageName = packageJson.name; + removePackage(tree); + + removeProjectConfiguration(tree, name); + removeFromBuildAll(tree); + + updateReadMe(tree, getUpdatedPackages(tree)); + + for (const t of getDemoTypes()) { + const demoAppRoot = getDemoAppRoot(t); + removeDemoFiles(tree, t, demoAppRoot); + removeFromDemoIndex(tree, t, demoAppRoot); + updateDemoDependencies(tree, demoAppRoot); + } + + removeSharedDemoFiles(tree); + updateDemoSharedIndex(tree); + + console.log(`"${npmPackageName}" removed from 'packages' and removed from all demo apps.`); + } else { + console.error(`Nothing found at 'packages/${name}/package.json' to remove.`) } - - removeSharedDemoFiles(tree); - updateDemoSharedIndex(tree); - - console.log(`"${getNpmScope()}/${name}" removed from 'packages' and removed from all demo apps.`); } function removePackage(tree: Tree) { @@ -165,7 +173,7 @@ export function updateDemoDependencies(tree: Tree, demoAppRoot: string) { updateJson(tree, packagePath, (json) => { json.dependencies = json.dependencies || {}; - delete json.dependencies[`${getNpmScope()}/${name}`]; + delete json.dependencies[npmPackageName]; return json; }); } diff --git a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_angular/__name__.component.ts__tmpl__ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_angular/__name__.component.ts__tmpl__ index e657dd8..535f789 100644 --- a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_angular/__name__.component.ts__tmpl__ +++ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_angular/__name__.component.ts__tmpl__ @@ -1,6 +1,6 @@ import { Component, NgZone } from '@angular/core'; import { DemoShared<%= stringUtils.classify(name) %> } from '@demo/shared'; -import { } from '<%= npmScope %>/<%= name %>'; +import { } from '<%= npmPackageName %>'; @Component({ selector: 'demo-<%= name %>', diff --git a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_demo_shared/__name__/index.ts__tmpl__ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_demo_shared/__name__/index.ts__tmpl__ index 9182852..a5f71ea 100644 --- a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_demo_shared/__name__/index.ts__tmpl__ +++ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_demo_shared/__name__/index.ts__tmpl__ @@ -1,5 +1,5 @@ import { DemoSharedBase } from '../utils'; -import { } from '<%= npmScope %>/<%= name %>'; +import { } from '<%= npmPackageName %>'; export class DemoShared<%= stringUtils.classify(name) %> extends DemoSharedBase { diff --git a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_react/__name__.tsx__tmpl__ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_react/__name__.tsx__tmpl__ index 1cc9ad9..b6ee9c3 100644 --- a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_react/__name__.tsx__tmpl__ +++ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_react/__name__.tsx__tmpl__ @@ -2,7 +2,7 @@ import * as React from "react"; import { Dialogs } from '@nativescript/core'; import { StyleSheet } from "react-nativescript"; import { DemoShared<%= stringUtils.classify(name) %> } from '@demo/shared'; -import { } from '<%= npmScope %>/<%= name %>'; +import { } from '<%= npmPackageName %>'; export function HomeScreen() { return ( diff --git a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_svelte/__name__.svelte__tmpl__ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_svelte/__name__.svelte__tmpl__ index 440c11b..9ddfa5b 100644 --- a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_svelte/__name__.svelte__tmpl__ +++ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_svelte/__name__.svelte__tmpl__ @@ -7,7 +7,7 @@ diff --git a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_vue/__name__.vue__tmpl__ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_vue/__name__.vue__tmpl__ index 50e934b..4a83e26 100644 --- a/packages/plugin-tools/src/generators/sync-packages-with-demos/files_vue/__name__.vue__tmpl__ +++ b/packages/plugin-tools/src/generators/sync-packages-with-demos/files_vue/__name__.vue__tmpl__ @@ -13,7 +13,7 @@