-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add prompts to add nx to angular cli (#1254)
- Loading branch information
Showing
21 changed files
with
316 additions
and
12 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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
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,7 @@ | ||
# vscode-nx-conversion | ||
|
||
This library was generated with [Nx](https://nx.dev). | ||
|
||
## Running unit tests | ||
|
||
Run `nx test vscode-nx-conversion` to execute the unit tests via [Jest](https://jestjs.io). |
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,15 @@ | ||
module.exports = { | ||
displayName: 'vscode-nx-conversion', | ||
preset: '../../../jest.preset.js', | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
}, | ||
}, | ||
testEnvironment: 'node', | ||
transform: { | ||
'^.+\\.[tj]sx?$': 'ts-jest', | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], | ||
coverageDirectory: '../../../coverage/libs/vscode/nx-conversion', | ||
}; |
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,4 @@ | ||
{ | ||
"name": "@nx-console/vscode/nx-conversion", | ||
"version": "0.0.1" | ||
} |
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,34 @@ | ||
{ | ||
"root": "libs/vscode/nx-conversion", | ||
"sourceRoot": "libs/vscode/nx-conversion/src", | ||
"projectType": "library", | ||
"targets": { | ||
"build": { | ||
"executor": "@nrwl/node:package", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"outputPath": "dist/libs/vscode/nx-conversion", | ||
"tsConfig": "libs/vscode/nx-conversion/tsconfig.lib.json", | ||
"packageJson": "libs/vscode/nx-conversion/package.json", | ||
"main": "libs/vscode/nx-conversion/src/index.ts", | ||
"assets": ["libs/vscode/nx-conversion/*.md"] | ||
} | ||
}, | ||
"lint": { | ||
"executor": "@nrwl/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["libs/vscode/nx-conversion/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nrwl/jest:jest", | ||
"outputs": ["coverage/libs/vscode/nx-conversion"], | ||
"options": { | ||
"jestConfig": "libs/vscode/nx-conversion/jest.config.js", | ||
"passWithNoTests": true | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
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 @@ | ||
export * from './lib/vscode-nx-conversion'; |
159 changes: 159 additions & 0 deletions
159
libs/vscode/nx-conversion/src/lib/vscode-nx-conversion.ts
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,159 @@ | ||
import { getShellExecutionForConfig, getTelemetry } from '@nx-console/server'; | ||
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration'; | ||
import { pipe, Subject } from 'rxjs'; | ||
import { filter, scan, tap } from 'rxjs/operators'; | ||
import { | ||
commands, | ||
ExtensionContext, | ||
ExtensionMode, | ||
Task, | ||
tasks, | ||
TaskScope, | ||
window, | ||
} from 'vscode'; | ||
|
||
/** | ||
* The amount of times the user should run commands before a prompt is shown. | ||
* | ||
* DevelopmentMode will change this to `1`. | ||
*/ | ||
const PROMPT_COUNT = 5; | ||
const PROMPT_MSG = | ||
'Would you like to make ng commands faster by adding computation caching? [Learn more here](https://nx.dev/migration/migration-angular?utm_source=vscode-nx-conversion&utm_medium=prompt&utm_campaign=vscode-nx-conversion)'; | ||
const PROMPT_MSG_DONT_ASK_AGAIN = | ||
'If you change your mind, you can run the `make ng faster with Nx` command through the command palette.'; | ||
|
||
/** | ||
* Singleton class for helping with Nx Conversion. | ||
* | ||
* Get instances with `NxConversion.instance` | ||
*/ | ||
export class NxConversion { | ||
private static _instance: NxConversion; | ||
|
||
public static get instance(): NxConversion { | ||
if (!NxConversion._instance) { | ||
throw 'NxConversion not created yet, please create an instance first with `NxConversion.createInstance(context)`'; | ||
} | ||
return NxConversion._instance; | ||
} | ||
private static set instance(value: NxConversion) { | ||
if (NxConversion._instance) { | ||
console.warn( | ||
'NxConversion already created, are you sure you want to create another instance?' | ||
); | ||
} else { | ||
commands.registerCommand('nxConsole.makeNgFaster', makeNgFaster); | ||
} | ||
|
||
NxConversion._instance = value; | ||
} | ||
|
||
private _listener = new Subject<string>(); | ||
|
||
private constructor(private _context: ExtensionContext) { | ||
const initialSeed = WorkspaceConfigurationStore.instance.get( | ||
'nxConversionCount', | ||
0 | ||
); | ||
|
||
const promptCount = | ||
_context.extensionMode === ExtensionMode.Development ? 1 : PROMPT_COUNT; | ||
|
||
this._listener | ||
.pipe(shouldPromptForConversion(initialSeed, promptCount, _context)) | ||
.subscribe(async (count) => { | ||
makeNgFaster(count); | ||
}); | ||
} | ||
|
||
static createInstance(context: ExtensionContext) { | ||
NxConversion.instance = new NxConversion(context); | ||
return NxConversion.instance; | ||
} | ||
|
||
async trackEvent(eventName: string) { | ||
if (eventName === 'generate') { | ||
return; | ||
} | ||
|
||
this._listener.next(eventName); | ||
} | ||
} | ||
|
||
async function makeNgFaster(count = 0) { | ||
getTelemetry().screenViewed('Convert to Nx'); | ||
|
||
const options = ['Yes', 'No']; | ||
if (count >= PROMPT_COUNT * 2) { | ||
options.push(`Don't ask again`); | ||
} | ||
|
||
const answer = await window.showInformationMessage(PROMPT_MSG, ...options); | ||
|
||
if (answer === undefined) { | ||
return; | ||
} | ||
|
||
if (answer === 'Yes') { | ||
getTelemetry().screenViewed('Convert to Nx - Yes'); | ||
getTelemetry().featureUsed('makeNgFaster'); | ||
tasks.executeTask(createMakeNgFasterTask()); | ||
} else if (answer === 'No') { | ||
getTelemetry().screenViewed('Convert to Nx - No '); | ||
getTelemetry().featureUsed('do-not makeNgFaster'); | ||
} else { | ||
WorkspaceConfigurationStore.instance.set('nxConversionDoNotAskAgain', true); | ||
getTelemetry().featureUsed('do-not-ask-again makeNgFaster'); | ||
window.showInformationMessage(PROMPT_MSG_DONT_ASK_AGAIN); | ||
} | ||
} | ||
|
||
function createMakeNgFasterTask() { | ||
const workspacePath = WorkspaceConfigurationStore.instance.get( | ||
'nxWorkspacePath', | ||
'' | ||
); | ||
const displayCommand = 'make-angular-cli-faster'; | ||
const task = new Task( | ||
{ | ||
type: 'nx', | ||
}, | ||
TaskScope.Workspace, | ||
displayCommand, | ||
'nx', | ||
getShellExecutionForConfig({ | ||
cwd: workspacePath, | ||
displayCommand, | ||
}) | ||
); | ||
return task; | ||
} | ||
|
||
function shouldPromptForConversion( | ||
initialSeed: number, | ||
promptCount: number, | ||
_context: ExtensionContext | ||
) { | ||
const askAgain: boolean = | ||
!WorkspaceConfigurationStore.instance.get( | ||
'nxConversionDoNotAskAgain', | ||
false | ||
) || _context.extensionMode === ExtensionMode.Development; | ||
|
||
return pipe( | ||
scan((acc) => acc + 1, initialSeed), | ||
tap((value) => { | ||
WorkspaceConfigurationStore.instance.set('nxConversionCount', value); | ||
}), | ||
filter((count) => count % promptCount === 0), | ||
filter( | ||
() => | ||
askAgain && | ||
(WorkspaceConfigurationStore.instance.get( | ||
'workspaceType', | ||
'' | ||
) as string) === 'angular' | ||
) | ||
); | ||
} |
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,13 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
}, | ||
{ | ||
"path": "./tsconfig.spec.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,11 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"outDir": "../../../dist/out-tsc", | ||
"declaration": true, | ||
"types": ["node"] | ||
}, | ||
"exclude": ["**/*.spec.ts", "**/*.test.ts"], | ||
"include": ["**/*.ts"] | ||
} |
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,19 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../../dist/out-tsc", | ||
"module": "commonjs", | ||
"types": ["jest", "node"] | ||
}, | ||
"include": [ | ||
"**/*.test.ts", | ||
"**/*.spec.ts", | ||
"**/*.test.tsx", | ||
"**/*.spec.tsx", | ||
"**/*.test.js", | ||
"**/*.spec.js", | ||
"**/*.test.jsx", | ||
"**/*.spec.jsx", | ||
"**/*.d.ts" | ||
] | ||
} |
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
Oops, something went wrong.