Skip to content

Commit cab786d

Browse files
committed
fix: env work without s.yaml
Signed-off-by: zxypro1 <1018995004@qq.com>
1 parent 95ec7c5 commit cab786d

File tree

5 files changed

+94
-27
lines changed

5 files changed

+94
-27
lines changed

__tests__/env.test.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ENVIRONMENT_FILE_NAME } from '@serverless-devs/parse-spec';
66
import { find, get } from 'lodash';
77
const s = path.resolve(__dirname, process.platform === 'win32' ? '../bin/s.cmd' : '../bin/s');
88
const cwd = path.resolve(__dirname, './fixtures/env');
9+
const cwdEmpty = path.resolve(__dirname, './fixtures/empty');
910

1011
test('set', async () => {
1112
const res = spawnSync(s, ['env', 'set', '--component', 'ServerlessDevsAdmin@dev'], { cwd });
@@ -183,4 +184,57 @@ test('list v2', async () => {
183184
const stdout = res.stdout.toString();
184185
console.log(stdout);
185186
expect(stdout).toContain('Not support template');
186-
})
187+
})
188+
189+
test('init with no s.yaml', async () => {
190+
const name = 'dev';
191+
const args = [
192+
'--name',
193+
name,
194+
'--project',
195+
'custom-project',
196+
'--description',
197+
'this is a description',
198+
'--type',
199+
'testing',
200+
'--overlays',
201+
'{"components":{"fc3test":{"region":"hangzhou"}}}',
202+
'--debug',
203+
];
204+
const res = spawnSync(s, ['env', 'init', ...args], { cwd: cwdEmpty, stdio: 'inherit' });
205+
console.log(res);
206+
expect(res.status).toBe(0);
207+
expect(fs.existsSync(path.join(cwd, 'env.yaml'))).toBeTruthy();
208+
});
209+
210+
test('describe with no s.yaml', async () => {
211+
const name = 'dev';
212+
const args = ['--name', name];
213+
const res = spawnSync(s, ['env', 'describe', ...args], { cwd: cwdEmpty, stdio: 'inherit' });
214+
console.log(res);
215+
expect(res.status).toBe(0);
216+
expect(res).toContain('this is a description');
217+
});
218+
219+
test('list with no s.yaml', async () => {
220+
const res = spawnSync(s, ['env', 'list'], { cwd: cwdEmpty, stdio: 'inherit' });
221+
console.log(res);
222+
expect(res.status).toBe(0);
223+
expect(res).toContain('this is a description');
224+
});
225+
226+
test('default with no s.yaml', async () => {
227+
const res = spawnSync(s, ['env', 'default'], { cwd: cwdEmpty, stdio: 'inherit' });
228+
console.log(res);
229+
expect(res.status).toBe(1);
230+
expect(res).toContain('the s.yaml/s.yml file was not found.');
231+
});
232+
233+
test('destroy with no s.yaml', async () => {
234+
const name = 'dev';
235+
const args = ['--name', name];
236+
const res = spawnSync(s, ['env', 'destroy', ...args], { cwd: cwdEmpty, stdio: 'inherit' });
237+
console.log(res);
238+
expect(res.status).toBe(0);
239+
expect(res).toContain('The environment dev was destroyed successfully');
240+
});

__tests__/fixtures/empty/test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
empty file path

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@
6666
"devDependencies": {
6767
"@serverless-devs/core": "^0.1.67-beta.1",
6868
"@serverless-devs/credential": "^0.0.9-beta.1",
69-
"@serverless-devs/engine": "^0.1.4-beta.13",
69+
"@serverless-devs/engine": "^0.1.4-beta.14",
7070
"@serverless-devs/load-application": "^0.0.14-beta.9",
7171
"@serverless-devs/load-component": "^0.0.8-beta.1",
7272
"@serverless-devs/logger": "^0.0.5",
73-
"@serverless-devs/parse-spec": "^0.0.28-beta.10",
73+
"@serverless-devs/parse-spec": "^0.0.28-beta.11",
7474
"@serverless-devs/registry": "^0.0.10",
7575
"@serverless-devs/secret": "^0.0.2-beta.5",
7676
"@serverless-devs/utils": "^0.0.16-beta.1",

src/utils/index.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,18 @@ export const getSchema = async (componentName: string) => {
229229
// 检查模版是否为3.x版本
230230
export const checkTemplateVersion = async (program: Command): Promise<boolean> => {
231231
const { template } = program.optsWithGlobals();
232-
const spec = await new ParseSpec(template, { logger }).start();
233-
if (!get(spec, 'yaml.use3x')) {
234-
logger.tips(`Not support template: ${get(spec, 'yaml.path')}, you can update template to 3.x version`);
235-
return false;
232+
try {
233+
const spec = await new ParseSpec(template, { logger }).start();
234+
if (!get(spec, 'yaml.use3x')) {
235+
logger.tips(`Not support template: ${get(spec, 'yaml.path')}, you can update template to 3.x version`);
236+
return false;
237+
}
238+
return true;
239+
} catch {
240+
// fix: env work without s.yaml
241+
logger.debug('no s.yaml, env work without s.yaml');
242+
return true;
236243
}
237-
return true;
238244
}
239245

240246
export const mount = async (module: string, program: Command) => {
@@ -248,7 +254,13 @@ export const mountAsync = async (module: string, program: Command) => {
248254
}
249255

250256
export const getEnvFilePath = async (template: string): Promise<string> => {
251-
const spec = await new ParseSpec(template, { logger }).start();
252-
const envPath = utils.getAbsolutePath(get(spec, 'yaml.content.env', ENVIRONMENT_FILE_NAME));
253-
return envPath;
257+
try {
258+
const spec = await new ParseSpec(template, { logger }).start();
259+
const envPath = utils.getAbsolutePath(get(spec, 'yaml.content.env', ENVIRONMENT_FILE_NAME));
260+
return envPath;
261+
} catch (error) {
262+
logger.debug('no template file, use default env.yaml');
263+
// fix: use default env.yaml
264+
return utils.getAbsolutePath(ENVIRONMENT_FILE_NAME);
265+
}
254266
}

0 commit comments

Comments
 (0)