Skip to content

Commit 6fcca24

Browse files
committed
Fixed errors and bugs
1 parent af041d5 commit 6fcca24

9 files changed

Lines changed: 38 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,12 @@ describe the command here
4848

4949
```
5050
USAGE
51-
$ codify apply [FILE] [-f] [-n <value>] [-p <value>]
51+
$ codify apply [FILE] [-p <value>]
5252
5353
ARGUMENTS
5454
FILE file to read
5555
5656
FLAGS
57-
-f, --force
58-
-n, --name=<value> name to print
5957
-p, --path=<value> path to project
6058
6159
DESCRIPTION

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"ink": "^4.4.1",
1616
"@inkjs/ui": "^1.0.0",
1717
"react": "^18.3.1",
18-
"chalk": "^5.3.0"
18+
"chalk": "^5.3.0",
19+
"parse-json": "^8.1.0"
1920
},
2021
"description": "Codify is a set up as code tool for developers",
2122
"devDependencies": {

src/commands/apply/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ export default class Apply extends Command {
4040
if (planResult.plan.every((p) => p.operation === ResourceOperation.NOOP)) {
4141
console.log('No changes necessary. Exiting');
4242
await planResult.pluginCollection.destroy();
43-
return this.exit(0);
43+
return process.exit(0);
4444
}
4545

4646
const confirm = await reporter.promptApplyConfirmation()
4747
if (!confirm) {
48-
return this.exit(0);
48+
return process.exit(0);
4949
}
5050

5151
await ApplyOrchestrator.run(planResult);
5252
} catch (error: unknown) {
5353
console.error(error);
5454
}
5555

56-
this.exit(0);
56+
process.exit(0);
5757
}
5858
}

src/commands/plan/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ export default class Plan extends Command {
4242
console.error(error);
4343
}
4444

45-
this.exit(0);
45+
process.exit(0);
4646
}
4747
}

src/parser/parser/json/file-parser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import parseJson from 'parse-json';
2+
13
import { ConfigBlock } from '../../../entities/index.js';
24
import { InternalError, JsonFileParseError, SyntaxError } from '../../../utils/errors.js';
35
import { File } from '../../reader/entities/file.js';
@@ -17,7 +19,7 @@ export class JsonFileParser implements FileParser {
1719

1820
private parseJson(file: File): unknown {
1921
try {
20-
return JSON.parse(file.contents);
22+
return parseJson(file.contents);
2123
} catch (error) {
2224
throw new JsonFileParseError({
2325
fileName: file.fileName,

src/plugins/plugin-process.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,23 @@ export class PluginProcess {
1919
this.process = process;
2020
}
2121

22-
static async start(jsFileDir: string): Promise<PluginProcess> {
22+
static async start(pluginPath: string, name: string): Promise<PluginProcess> {
23+
const isTypescript = pluginPath.endsWith('.ts');
24+
const isTsxInstalled = PluginProcess.isTsxInstalled();
25+
26+
if (isTypescript && !isTsxInstalled) {
27+
throw new Error('Typescript plugins are only allowed for dev mode. TS plugins are not allowed for production');
28+
}
29+
30+
ctx.log(`Starting plugin ${name}`);
31+
2332
const _process = fork(
24-
jsFileDir,
33+
pluginPath,
2534
[],
2635
{
2736
env: { ...process.env, FORCE_COLOR: '1' },
28-
execArgv: ['--import', 'tsx'],
29-
silent: true
37+
silent: true,
38+
...(isTypescript && { execArgv: ['--import', 'tsx'] }),
3039
},
3140
);
3241

@@ -53,6 +62,17 @@ export class PluginProcess {
5362
sendMessage(message: PluginMessage): void {
5463
this.process.send(message);
5564
}
65+
66+
// Tsx is only installed for dev builds. Only allow typescript plugins for testing.
67+
private static isTsxInstalled(): boolean {
68+
try {
69+
require.resolve('tsx');
70+
} catch (e) {
71+
return false;
72+
}
73+
74+
return true;
75+
}
5676
}
5777

5878
class SendMessageForResultHandler {

src/plugins/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class Plugin {
3131
}
3232

3333
async initialize(): Promise<InitializeResponseData> {
34-
this.process = await PluginProcess.start(this.path);
34+
this.process = await PluginProcess.start(this.path, this.name);
3535

3636
const initializeResponse = await this.process.sendMessageForResult({ cmd: 'initialize', data: {} });
3737

src/plugins/resolver.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'node:path';
44
import { Readable } from 'node:stream';
55
import { finished } from 'node:stream/promises';
66

7+
import { ctx } from '../events/context.js';
78
import { Plugin } from './plugin.js';
89

910
const DEFAULT_PLUGIN_URL = 'https://codify-plugin-library.s3.amazonaws.com/codify-core/index.js';
@@ -65,6 +66,7 @@ export class PluginResolver {
6566
try {
6667
pluginDirStat = await fs.stat(PLUGIN_CACHE_DIR)
6768
} catch {
69+
ctx.log('Plugin cache dir does not exist')
6870
}
6971

7072
if (pluginDirStat && pluginDirStat.isDirectory()) {
@@ -75,6 +77,7 @@ export class PluginResolver {
7577
throw new Error(`An object already exists at ${PLUGIN_CACHE_DIR} and is not a directory. Please delete and try again`);
7678
}
7779

80+
ctx.log('Creating a new cache dir for codify');
7881
await fs.mkdir(PLUGIN_CACHE_DIR, { recursive: true });
7982
}
8083
}

src/ui/components/plan/plan.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export function PlanComponent(props: {
99
plan: PlanResponseData[]
1010
}) {
1111
const filteredPlan = props.plan.filter((p) => p.operation !== ResourceOperation.NOOP);
12-
// console.log(JSON.stringify(props.plan, null, 2));
1312

1413
return <Box flexDirection="column">
1514
<Box borderStyle="round" borderColor="green">

0 commit comments

Comments
 (0)