Skip to content

Commit

Permalink
init, sync, list, goto moved under project commands
Browse files Browse the repository at this point in the history
  • Loading branch information
randilfernando committed Apr 1, 2024
1 parent 22f7f70 commit 2cdffb8
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 238 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
- To start using cpm, follow the [getting started](docs/getting-started) guide. Get your projects organized and managed efficiently in no time!

## Commands
- [cpm](docs/core)
- [cpm plugin](docs/plugin)
- [cpm project](docs/project)
- [cpm flow](docs/flow)
- [cpm template](docs/template)
- [cpm repo](docs/repo)
- [cpm task](docs/task)
- [cpm pr](docs/pr)
- [cpm plugin](docs/plugin)
- [cpm flow](docs/flow)

## Workflow management
To start using workflows, follow the [workflow](docs/workflow) guide. Streamline your processes effortlessly with automation—unlocking more time for what truly matters.
Expand Down
41 changes: 0 additions & 41 deletions docs/core/README.md

This file was deleted.

57 changes: 57 additions & 0 deletions docs/project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
### Core commands

#### project init

Initialize a cpm project.

```bash
cpm project init
```

#### project list

List projects.

```bash
cpm project list
```

#### project clone

Clone a repository to the root directory configured in ctx.config.rootDir.

```bash
cpm repo clone <url>
```

**Arguments:**
- `url`: URL of the git repository.

**Outputs:**
- `org`: Organization name extracted from URL.
- `repo`: Repository name extracted from URL.
- `path`: Locally cloned directory.

#### project goto

Go to project folder. If there are multiple repositories available for given query this will prompt user to select.

```bash
cpm project goto <query>
```

**Arguments:**
- `query`: Pattern to search. Example: `cpm find Cloudimpl-Inc/cpm`

**Outputs:**
- `org`: Organization name extracted from URL.
- `repo`: Repository name extracted from URL.
- `path`: Locally cloned directory.

#### project sync

Sync project.

```bash
cpm prtoject sync
```
10 changes: 4 additions & 6 deletions docs/repo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

#### repo clone

Clone a git repository to the root directory configured in ctx.config.rootDir.
Clone a repository.

```bash
cpm repo clone <url>
```

**Arguments:**
- `url`: URL of the git repository.
- `url`: URL of the repository.

**Outputs:**
- `org`: Organization name extracted from URL.
- `repo`: Repository name extracted from URL.
- `path`: Locally cloned directory.
**Options:**
- `-d, --destination <destination>`: Destination to clone (optional).

#### repo checkout

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cloudimpl-inc/cpm",
"version": "2.32.0",
"version": "2.33.0",
"description": "CloudImpl Project Manager",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
12 changes: 6 additions & 6 deletions src/bin/cpm.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/bash

# Check if the first argument is 'goto'
if [ "$1" = "goto" ]; then
# If 'goto' command is provided, change directory
if [ $# -gt 1 ]; then
# Check if the first and second arguments are 'project' and 'goto'
if [ "$1" = "project" ] && [ "$2" = "goto" ]; then
# If 'project' and 'goto' commands are provided, continue with the rest of the script
if [ $# -gt 2 ]; then
# Define the output file path using the CPM_OUTPUT environment variable
output_file=$(mktemp)

# Execute the cpmjs find command and redirect its output to the specified output file
env CPM_OUTPUT="$output_file" cpmjs goto "$2" || return
env CPM_OUTPUT="$output_file" cpmjs project goto "$3" || return

# Read the output file and set environment variables
while IFS='=' read -r key value || [ -n "$key" ]; do
Expand All @@ -25,7 +25,7 @@ if [ "$1" = "goto" ]; then
rm "$output_file"
else
# Show cpm error message
cpmjs goto
cpmjs project goto
fi
else
# Run your Node.js CLI tool with arguments passed to the wrapper script
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import RootPlugin from "./root-plugin";
import ProjectPlugin from "./project-plugin";
import FlowPlugin from "./flow-plugin";
import TemplatePlugin from "./template-plugin";

Expand All @@ -7,6 +8,10 @@ const plugins = [
name: 'inbuilt/root',
creator: RootPlugin
},
{
name: 'inbuilt/project',
creator: ProjectPlugin
},
{
name: 'inbuilt/flow',
creator: FlowPlugin
Expand Down
131 changes: 131 additions & 0 deletions src/plugins/project-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {Action, ActionOutput, CPMConfig, CPMContext, CPMPlugin} from "../index";
import {
configFilePath,
createFile,
createFolder, executeShellCommand,
folderPath, getSelection,
gitIgnoreFilePath,
isProjectRepo, syncProject,
writeJson,
writeYaml
} from "../util";
import chalk from "chalk";
import {appendFileSync, readdirSync} from "fs";
import path from "path";
import fs from "fs";

const init: Action = async (ctx, input) => {
if (isProjectRepo) {
console.log(chalk.yellow('already initialized'));
} else {
const config: CPMConfig = {
plugins: []
}
writeYaml(configFilePath, config);

createFolder(folderPath);
writeJson(`${folderPath}/package.json`, {});

createFile(gitIgnoreFilePath, '');
appendFileSync(gitIgnoreFilePath,
'\n# cpm\n' +
'.cpm/node_modules\n' +
'.cpm/_*\n',
)

console.log(chalk.green('cpm project initialized'));
}
return {};
};

const list: Action = (ctx, input) => {
readdirSync(ctx.config.rootDir, {withFileTypes: true})
.filter(orgDir => orgDir.isDirectory())
.forEach(orgDir => {
console.log(`|--${orgDir.name}`)
readdirSync(`${orgDir.path}/${orgDir.name}`, {withFileTypes: true})
.filter(repoDir => repoDir.isDirectory())
.forEach(repoDir => console.log(`| |--${repoDir.name} => ${repoDir.path}/${repoDir.name}`))
})

return {};
};

const clone: Action = async (ctx, input): Promise<ActionOutput> => {
const {url} = input.args;

if (!url.startsWith('http')) {
console.log(chalk.red('Only support http/https urls'));
return {};
}

const [org, repo] = url.split('/').slice(-2).map((segment: string) => segment.replace('.git', ''));
const repoDir = path.join(ctx.config.rootDir, org, repo);

// Check if the repository is already cloned
if (fs.existsSync(repoDir)) {
console.log(chalk.yellow(`Repository already exists at ${repoDir}. Skipping clone step.`));
return {org, repo, path: repoDir};
}

await executeShellCommand(`cpm repo clone ${url} ${repoDir}`);
return {org, repo, path: repoDir};
}

const goTo: Action = async (ctx, input) => {
const {query} = input.args;
const filtered: {id: string, name: string, org: string, repo: string, path: string}[] = [];

for (const orgDir of readdirSync(ctx.config.rootDir, {withFileTypes: true})) {
if (!orgDir.isDirectory()) {
continue;
}

const org = orgDir.name;

for (const repoDir of readdirSync(`${orgDir.path}/${orgDir.name}`, {withFileTypes: true})) {
const repo = repoDir.name;
const repoNameFull = `${orgDir.name}/${repoDir.name}`;
const path = `${repoDir.path}/${repoDir.name}`;

if (orgDir.isDirectory() && repoNameFull.toLowerCase().includes(query.toLowerCase())) {
filtered.push({id: path, name: `${org}/${repo}`, org, repo, path});
}
}
}

if (filtered.length === 0) {
console.log(chalk.red('repository not found'));
return {};
} else if (filtered.length === 1) {
const result = filtered[0];
console.log(chalk.green(result.path));
return result;
} else {
const selection = await getSelection('Select repository:', filtered);
const result = filtered.find(f => f.id === selection)!;
console.log(chalk.green(result.path));
return result;
}
};

const sync: Action = async (ctx, input) => {
await syncProject(ctx.config);
await executeShellCommand('cpm repo sync');
return {};
};

const projectPlugin: CPMPlugin = {
name: 'project',
actions: {
'project init': init,
'project list': list,
'project clone': clone,
'project goto': goTo,
'project sync': sync
}
};

export default function createProjectPlugin(ctx: CPMContext): CPMPlugin {
return projectPlugin;
}
Loading

0 comments on commit 2cdffb8

Please sign in to comment.