Skip to content

Commit 3f2104e

Browse files
authored
feat: Add --dockerfile flag to apify init (#989)
Closes #76
1 parent 6864004 commit 3f2104e

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/commands/init.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export class InitCommand extends ApifyCommand<typeof InitCommand> {
3737
'Automatic yes to prompts; assume "yes" as answer to all prompts. Note that in some cases, the command may still ask for confirmation.',
3838
required: false,
3939
}),
40+
dockerfile: Flags.string({
41+
description: 'Path to a Dockerfile to use for the Actor (e.g., "./Dockerfile" or "./docker/Dockerfile").',
42+
required: false,
43+
}),
4044
};
4145

4246
async run() {
@@ -111,8 +115,21 @@ export class InitCommand extends ApifyCommand<typeof InitCommand> {
111115
}
112116

113117
// Migrate apify.json to .actor/actor.json
114-
const localConfig = { ...EMPTY_LOCAL_CONFIG, ...actorConfig.unwrap().config };
115-
await setLocalConfig(Object.assign(localConfig, { name: actorName }), cwd);
118+
const localConfig = {
119+
...EMPTY_LOCAL_CONFIG,
120+
...actorConfig.unwrap().config,
121+
};
122+
const configToWrite: Record<string, unknown> = {
123+
...localConfig,
124+
name: actorName,
125+
};
126+
127+
// Add dockerfile field if provided
128+
if (this.flags.dockerfile) {
129+
configToWrite.dockerfile = this.flags.dockerfile;
130+
}
131+
132+
await setLocalConfig(configToWrite, cwd);
116133
}
117134

118135
await setLocalEnv(cwd);

test/local/commands/init.test.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ describe('apify init', () => {
2828
});
2929

3030
it('correctly creates basic structure with empty INPUT.json', async () => {
31-
await testRunCommand(InitCommand, { args_actorName: actName, flags_yes: true });
31+
await testRunCommand(InitCommand, {
32+
args_actorName: actName,
33+
flags_yes: true,
34+
});
3235

3336
// Check that it won't create deprecated config
3437
// TODO: We can remove this later
@@ -62,11 +65,17 @@ describe('apify init', () => {
6265
required: ['url'],
6366
};
6467

65-
const defaultActorJson = Object.assign(EMPTY_LOCAL_CONFIG, { name: actName, input });
68+
const defaultActorJson = Object.assign(EMPTY_LOCAL_CONFIG, {
69+
name: actName,
70+
input,
71+
});
6672

6773
await mkdir(joinPath('.actor'), { recursive: true });
6874
writeFileSync(joinPath(LOCAL_CONFIG_PATH), JSON.stringify(defaultActorJson, null, '\t'), { flag: 'w' });
69-
await testRunCommand(InitCommand, { args_actorName: actName, flags_yes: true });
75+
await testRunCommand(InitCommand, {
76+
args_actorName: actName,
77+
flags_yes: true,
78+
});
7079

7180
// Check that it won't create deprecated config
7281
// TODO: We can remove this later
@@ -79,4 +88,31 @@ describe('apify init', () => {
7988
),
8089
).toStrictEqual({ url: 'https://www.apify.com/' });
8190
});
91+
92+
it('correctly creates config with dockerfile when provided', async () => {
93+
const dockerfilePath = './Dockerfile';
94+
await testRunCommand(InitCommand, {
95+
args_actorName: actName,
96+
flags_yes: true,
97+
flags_dockerfile: dockerfilePath,
98+
});
99+
100+
const config = JSON.parse(readFileSync(joinPath(LOCAL_CONFIG_PATH), 'utf8'));
101+
expect(config).toMatchObject({
102+
...EMPTY_LOCAL_CONFIG,
103+
name: actName,
104+
dockerfile: dockerfilePath,
105+
});
106+
});
107+
108+
it('correctly creates config without dockerfile when not provided', async () => {
109+
await testRunCommand(InitCommand, {
110+
args_actorName: actName,
111+
flags_yes: true,
112+
});
113+
114+
const config = JSON.parse(readFileSync(joinPath(LOCAL_CONFIG_PATH), 'utf8'));
115+
expect(config).toStrictEqual(Object.assign(EMPTY_LOCAL_CONFIG, { name: actName }));
116+
expect(config.dockerfile).toBeUndefined();
117+
});
82118
});

0 commit comments

Comments
 (0)