-
-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Suggestions for executors #127
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,57 @@ | ||
import { ExecutorContext, logger } from '@nx/devkit' | ||
import { CliExecutorSchema } from './schema' | ||
import { | ||
isString, | ||
normalizeOptions, | ||
runCommands, | ||
stringifyFirebaseArgs, | ||
} from './utils' | ||
|
||
/** | ||
* @param options | ||
* @param context | ||
* @returns | ||
*/ | ||
export default async function runExecutor( | ||
options: CliExecutorSchema & { [key: string]: unknown }, | ||
context: Pick<ExecutorContext, 'root'>, | ||
) { | ||
logger.info('Starting Executor...') | ||
const normalizedOptions = await normalizeOptions(options, context) | ||
if (!normalizedOptions.args.project) return { success: false } | ||
|
||
const stringifiedArgs = stringifyFirebaseArgs( | ||
normalizedOptions._[0], | ||
normalizedOptions.args, | ||
) | ||
|
||
//:TODO remove | ||
if (normalizedOptions._[0].includes('emulators')) { | ||
if (isString(normalizedOptions.args.only)) { | ||
if (normalizedOptions.args.only.includes('auth')) { | ||
process.env.NX_REACT_APP_AUTH_EMULATOR = 'true' | ||
} | ||
if (normalizedOptions.args.only.includes('functions')) { | ||
process.env.NX_REACT_APP_FUNCTIONS_EMULATOR = 'true' | ||
} | ||
if (normalizedOptions.args.only.includes('firestore')) { | ||
process.env.NX_REACT_APP_FIRESTORE_EMULATOR = 'true' | ||
} | ||
if (normalizedOptions.args.only.includes('storage')) { | ||
process.env.NX_REACT_APP_STORAGE_EMULATOR = 'true' | ||
} | ||
} else { | ||
process.env.NX_REACT_APP_AUTH_EMULATOR = 'true' | ||
process.env.NX_REACT_APP_FUNCTIONS_EMULATOR = 'true' | ||
process.env.NX_REACT_APP_FIRESTORE_EMULATOR = 'true' | ||
process.env.NX_REACT_APP_STORAGE_EMULATOR = 'true' | ||
} | ||
} | ||
|
||
const command = ['firebase', ...normalizedOptions._, stringifiedArgs] | ||
.filter(Boolean) | ||
.join(' ') | ||
|
||
runCommands([command]) | ||
return { success: true } | ||
} |
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,6 @@ | ||
export interface CliExecutorSchema { | ||
command: string | ||
config: string | ||
project?: string | ||
_: string[] | ||
} |
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,36 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"version": 2, | ||
"title": "Firebase CLI executor", | ||
"description": "Runs the Firebase CLI for the target project's firebase configuration", | ||
"type": "object", | ||
"properties": { | ||
"config": { | ||
"type": "string", | ||
"description": "Path to the firebase configuration json for this project", | ||
"default": "firebase.json" | ||
}, | ||
"command": { | ||
"description": "Firebase CLI command", | ||
"type": "string", | ||
"format": "html-selector", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
} | ||
}, | ||
"project": { | ||
"type": "string", | ||
"description": "Name or alias from the .firebaserc of the firebase project being used" | ||
}, | ||
"_": { | ||
"type": "array", | ||
"items": { | ||
"anyOf": [{ "type": "string" }] | ||
}, | ||
"description": "Non-hyphenated options for firebase command", | ||
"default": [] | ||
} | ||
}, | ||
"required": ["config", "command"] | ||
} |
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,165 @@ | ||
import { execSync } from 'node:child_process' | ||
import { setTimeout } from 'node:timers/promises' | ||
import { Separator, select } from '@inquirer/prompts' | ||
import { ExecutorContext, joinPathFragments, logger } from '@nx/devkit' | ||
import parseHelp from 'parse-help' | ||
import yargs from 'yargs/yargs' | ||
import { CliExecutorSchema } from './schema' | ||
|
||
export const parseCommand = (command: string): string => { | ||
if (command[0] === '"' && command.at(-1) === '"') { | ||
return command.slice(1, -1) | ||
} | ||
return command | ||
} | ||
|
||
export const runCommand = (command: string): void => { | ||
execSync(parseCommand(command), { | ||
stdio: 'inherit', | ||
}) | ||
} | ||
|
||
export const runCommands = (commands: string[]): void => { | ||
for (const command of commands) { | ||
runCommand(command) | ||
} | ||
} | ||
|
||
export const isString = (u: unknown): u is string => { | ||
if (typeof u === 'string') return true | ||
return false | ||
} | ||
|
||
const getFirebaseProject = async ( | ||
project?: string, | ||
): Promise<string | undefined> => { | ||
if (project) return project | ||
let activeFirebaseProject = execSync( | ||
`echo $(echo $(grep "$(pwd)" ~/.config/configstore/firebase-tools.json | cut -d" " -f2)" " | sed -e 's/"//g')`, | ||
) | ||
.toString() | ||
.trim() | ||
|
||
if (!activeFirebaseProject) { | ||
logger.error( | ||
`No firebase project has been explicitly set and No active project. Please select desired project with 'firebase use' or select project when running command`, | ||
) | ||
|
||
return undefined | ||
} | ||
|
||
if (activeFirebaseProject.at(-1) === ',') { | ||
activeFirebaseProject = activeFirebaseProject.slice(0, -1) | ||
} | ||
|
||
const useActiveProject = select({ | ||
message: `Use active project (${activeFirebaseProject})?`, | ||
choices: [ | ||
{ | ||
name: 'Yes', | ||
value: true, | ||
}, | ||
{ | ||
name: 'No', | ||
value: false, | ||
}, | ||
new Separator(), | ||
], | ||
}) | ||
|
||
const defaultUseActiveProject = setTimeout(10000).then(() => { | ||
useActiveProject.cancel() | ||
return false | ||
}) | ||
|
||
const answer = await Promise.race([defaultUseActiveProject, useActiveProject]) | ||
|
||
if (!answer) { | ||
logger.error( | ||
`Select desired project with 'firebase use' or select project when running command`, | ||
) | ||
return undefined | ||
} | ||
|
||
return activeFirebaseProject | ||
} | ||
|
||
export const normalizeOptions = async ( | ||
options: CliExecutorSchema & { | ||
[key: string]: unknown | ||
}, | ||
context: Pick<ExecutorContext, 'root'>, | ||
): Promise<{ _: string[]; args: Record<string, unknown> }> => { | ||
const { command, config, _: params, project, ...additionalArgs } = options | ||
|
||
const { $0, _: commandParams, ...commandArgs } = await yargs(command).argv | ||
|
||
const [cCommand, ...cParams] = commandParams.map((value) => { | ||
return value.toString() | ||
}) | ||
|
||
if (cCommand === params[0]) { | ||
params.shift() | ||
} | ||
|
||
if (cParams.length > 0 && params.length > 0) { | ||
logger.warn('Conflicting parameters for firebase command') | ||
logger.warn(`Parameters to ignore [${cParams.toString()}]`) | ||
logger.warn(`Parameters to use [${params.toString()}]`) | ||
} | ||
|
||
const finalParams = [ | ||
cCommand, | ||
...(params ?? cParams).map((value) => { | ||
if (value[0] !== '"' && value.at(-1) !== '"' && value.includes(' ')) { | ||
return '"' + value + '"' | ||
} | ||
return value | ||
}), | ||
].filter(Boolean) | ||
|
||
//Set up args | ||
const finalArgs = { | ||
...commandArgs, | ||
...additionalArgs, | ||
} | ||
|
||
const finalArgsProject = | ||
typeof finalArgs.project === 'string' ? finalArgs.project : undefined | ||
|
||
const firebaseProject = await getFirebaseProject(project ?? finalArgsProject) | ||
finalArgs.project = firebaseProject | ||
finalArgs.config = joinPathFragments(context.root, config) | ||
|
||
return { | ||
_: finalParams, | ||
args: finalArgs, | ||
} | ||
} | ||
|
||
export const stringifyFirebaseArgs = ( | ||
command: string, | ||
args: Record<string, unknown>, | ||
): string => { | ||
const base = parseHelp(execSync('firebase --help')) | ||
const base2 = parseHelp(execSync(`firebase ${command} --help`)) | ||
|
||
const validArgs = Object.keys({ | ||
...base.flags, | ||
...base.aliases, | ||
...base2.flags, | ||
...base2.aliases, | ||
}) | ||
|
||
const argsToBeUsed = Object.keys(args) | ||
.filter((p) => validArgs.indexOf(p) !== -1) | ||
.reduce((m, c) => ((m[c] = args[c]), m), {}) | ||
|
||
return Object.keys(argsToBeUsed) | ||
.map((a) => | ||
argsToBeUsed[a] === true || argsToBeUsed[a] === 'true' | ||
? `--${a}` | ||
: `--${a}=${argsToBeUsed[a]}`, | ||
) | ||
.join(' ') | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/nx-firebase/src/executors/copy-local-files/executor.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,36 @@ | ||
import { copyFileSync, statSync } from 'fs' | ||
import path from 'path' | ||
import { ExecutorContext, joinPathFragments, logger } from '@nx/devkit' | ||
import { CopyLocalFilesExecutorSchema } from './schema' | ||
|
||
export default async function runExecutor( | ||
options: CopyLocalFilesExecutorSchema, | ||
context: ExecutorContext | ||
) { | ||
const projectName = context.projectName | ||
if (!projectName) return { success: false } | ||
const projectRoot = context.workspace?.projects[projectName].root | ||
const projectRootPath = `${context.root}/${projectRoot}` | ||
const projectDistPath = `${context.root}/dist/${projectRoot}` | ||
|
||
for (const file of ['.env.local', '.secret.local']) { | ||
const fileFullPath = joinPathFragments(projectRootPath, file) | ||
if (fileExists(fileFullPath)) { | ||
copyFileSync(fileFullPath, joinPathFragments(projectDistPath, file)) | ||
} | ||
} | ||
return { | ||
success: true, | ||
} | ||
} | ||
|
||
const fileExists = (path: string): boolean => { | ||
try { | ||
const isFile = statSync(path).isFile() | ||
if (!isFile) logger.warn(`'${path}' is not a file`) | ||
return isFile | ||
} catch (error) { | ||
logger.warn(error.message) | ||
return false | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/nx-firebase/src/executors/copy-local-files/schema.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export interface CopyLocalFilesExecutorSchema {} // eslint-disable-line |
9 changes: 9 additions & 0 deletions
9
packages/nx-firebase/src/executors/copy-local-files/schema.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,9 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"version": 2, | ||
"title": "CopyLocalFiles executor", | ||
"description": "", | ||
"type": "object", | ||
"properties": {}, | ||
"required": [] | ||
} |
Empty file.
47 changes: 47 additions & 0 deletions
47
packages/nx-firebase/src/executors/kill-emulator/executor.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,47 @@ | ||
import { execSync } from 'child_process' | ||
import { platform } from 'os' | ||
import { ExecutorContext, joinPathFragments, logger, readJsonFile } from '@nx/devkit' | ||
import { KillEmulatorPortsExecutorSchema } from './schema.js' | ||
|
||
export default async function runExecutor( | ||
options: KillEmulatorPortsExecutorSchema, | ||
context: ExecutorContext | ||
) { | ||
const normalizedPath = joinPathFragments(context.root, options.config) | ||
const emulatorJson = readJsonFile(normalizedPath) | ||
|
||
const emulators = emulatorJson.emulators as Record<string, unknown> | ||
|
||
const emulatorPorts: number[] = [] | ||
for (const entry of Object.entries(emulators)) { | ||
const value = entry[1] | ||
if (typeof value === 'object' && value && 'port' in value) { | ||
const port = Number(value.port) | ||
if (!isNaN(port)) emulatorPorts.push(port) | ||
} | ||
} | ||
|
||
const killFunc = platform() === 'win32' ? win32Kill : unixKill | ||
emulatorPorts.map(killFunc) | ||
|
||
return { | ||
success: true, | ||
} | ||
} | ||
|
||
function win32Kill(port: number) { | ||
logger.log( | ||
`${port} not killed... killing emulators through this method is not implemented for windows` | ||
) | ||
} | ||
|
||
function unixKill(port: number) { | ||
const pid = execSync(`lsof -t -i:${port} || echo ""`).toString().trim() | ||
if (!pid) { | ||
console.log(`no process running at port ${port}`) | ||
return | ||
} | ||
logger.log(`killing process at port ${port}...`) | ||
execSync(`kill -9 ${pid}`) | ||
logger.log(`killed process at port ${port}`) | ||
} |
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,3 @@ | ||
export interface KillEmulatorPortsExecutorSchema { | ||
config: string | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/nx-firebase/src/executors/kill-emulator/schema.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,14 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"version": 2, | ||
"title": "KillEmulatorPorts executor", | ||
"description": "", | ||
"type": "object", | ||
"properties": { | ||
"config": { | ||
"type": "string", | ||
"description": "Path to project's firebase config (where the emulator ports are defined)" | ||
} | ||
}, | ||
"required": ["config"] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is my current workaround for detecting if my frontend application should connect to the emulators when serving, but as you can see it is specific to cra applications. The frontend code then looks for these variables before in initialises each sdk.
Not sure if there is a more generic way of doing this/if it should be removed and handled elsewhere