-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds workflow:create command (#2678)
# Why Adds `eas workflow:create` to make it faster to create workflow files in a project. This command creates a file like **.eas/workflows/workflow.yml**. The user can choose the file name or can provide it as an arugment. # Screenshots <img width="552" alt="Screenshot 2024-11-08 at 8 50 58 AM" src="https://github.com/user-attachments/assets/5cf82e7e-cce6-4098-b291-fccdac25f103"> <img width="584" alt="Screenshot 2024-11-08 at 8 51 27 AM" src="https://github.com/user-attachments/assets/d6f6e7cb-18c1-420e-b52a-5bdd42446715"> <img width="617" alt="Screenshot 2024-11-08 at 8 52 04 AM" src="https://github.com/user-attachments/assets/81cc79cf-afb7-4855-bfce-0679f98655fa"> # Test Plan Make sure that you can run the command as pictured above and that it correctly creates the workflow file. Also make sure that the unit tests pass. # Future Once we have docs for workflows that are public, I'd like to put a link to the docs in the default file this command creates. For now, I am just putting Hello World as a reasonable placeholder. Co-authored-by: Szymon Dziedzic <[email protected]> Co-authored-by: Stanisław Chmiela <[email protected]>
- Loading branch information
1 parent
c7cfcaf
commit 4571910
Showing
2 changed files
with
128 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import chalk from 'chalk'; | ||
import fs from 'fs/promises'; | ||
import fsExtra from 'fs-extra'; | ||
import path from 'path'; | ||
import prompts from 'prompts'; | ||
|
||
import EasCommand from '../../commandUtils/EasCommand'; | ||
import { EASNonInteractiveFlag } from '../../commandUtils/flags'; | ||
import Log from '../../log'; | ||
import { WorkflowFile } from '../../utils/workflowFile'; | ||
|
||
const DEFAULT_WORKFLOW_NAME = 'workflow.yml'; | ||
const HELLO_WORLD_TEMPLATE = `name: Hello World | ||
on: | ||
push: | ||
branches: ['*'] | ||
jobs: | ||
hello_world: | ||
steps: | ||
- uses: eas/checkout | ||
- run: echo "Hello, World" | ||
`; | ||
|
||
export class WorkflowCreate extends EasCommand { | ||
static override description = 'create a new workflow configuration YAML file'; | ||
|
||
static override args = [ | ||
{ | ||
name: 'name', | ||
description: 'Name of the workflow file (must end with .yml or .yaml)', | ||
required: false, | ||
}, | ||
]; | ||
|
||
static override flags = { | ||
...EASNonInteractiveFlag, | ||
}; | ||
|
||
static override contextDefinition = { | ||
...this.ContextOptions.ProjectDir, | ||
}; | ||
|
||
async runAsync(): Promise<void> { | ||
const { | ||
args: { name: argFileName }, | ||
flags, | ||
} = await this.parse(WorkflowCreate); | ||
|
||
const { projectDir } = await this.getContextAsync(WorkflowCreate, { | ||
nonInteractive: flags['non-interactive'], | ||
}); | ||
|
||
let fileName = argFileName; | ||
|
||
if (!fileName) { | ||
const response = await prompts({ | ||
type: 'text', | ||
name: 'fileName', | ||
message: 'What would you like to name your workflow file?', | ||
initial: DEFAULT_WORKFLOW_NAME, | ||
validate: value => { | ||
try { | ||
WorkflowFile.validateYamlExtension(value); | ||
return true; | ||
} catch (error) { | ||
return error instanceof Error ? error.message : 'Invalid file name'; | ||
} | ||
}, | ||
}); | ||
|
||
if (!response.fileName) { | ||
Log.warn('Workflow creation cancelled.'); | ||
process.exit(0); | ||
} | ||
|
||
fileName = response.fileName; | ||
} | ||
|
||
try { | ||
await this.ensureWorkflowsDirectoryExistsAsync({ projectDir }); | ||
await this.createWorkflowFileAsync({ fileName, projectDir }); | ||
} catch (error) { | ||
Log.error('Failed to create workflow file.'); | ||
throw error; | ||
} | ||
} | ||
|
||
private async ensureWorkflowsDirectoryExistsAsync({ | ||
projectDir, | ||
}: { | ||
projectDir: string; | ||
}): Promise<void> { | ||
try { | ||
await fs.access(path.join(projectDir, '.eas', 'workflows')); | ||
} catch { | ||
await fs.mkdir(path.join(projectDir, '.eas', 'workflows'), { recursive: true }); | ||
Log.withTick(`Created directory ${chalk.bold(path.join(projectDir, '.eas', 'workflows'))}`); | ||
} | ||
} | ||
|
||
private async createWorkflowFileAsync({ | ||
fileName, | ||
projectDir, | ||
}: { | ||
fileName: string; | ||
projectDir: string; | ||
}): Promise<void> { | ||
WorkflowFile.validateYamlExtension(fileName); | ||
|
||
const filePath = path.join(projectDir, '.eas', 'workflows', fileName); | ||
|
||
if (await fsExtra.pathExists(filePath)) { | ||
throw new Error(`Workflow file already exists: ${filePath}`); | ||
} | ||
|
||
await fs.writeFile(filePath, HELLO_WORLD_TEMPLATE); | ||
Log.withTick(`Created ${chalk.bold(filePath)}`); | ||
} | ||
} |
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