Skip to content

Commit

Permalink
feat: load custom component.
Browse files Browse the repository at this point in the history
Signed-off-by: huyikun.hyk <[email protected]>
  • Loading branch information
huyikun.hyk committed Feb 21, 2024
1 parent 20706b9 commit a03d025
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/load-component/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class Component {
}
async update() {
const [componentName, componentVersion] = getProvider(this.name);
if(componentVersion === 'CUSTOM') {
this.params.logger(`Skip outside component's update. You could use 's clean --component <component_name>' before deploy to update your custom component cache manually.`);
return;
}
debug(`componentName: ${componentName}, componentVersion: ${componentVersion}`);
const componentCachePath = getComponentCachePath(componentName, componentVersion);
debug(`componentCachePath: ${componentCachePath}`);
Expand All @@ -42,18 +46,18 @@ export class Component {
private async getDevComponent() {
const [componentName, componentVersion] = getProvider(this.name);
debug(`componentName: ${componentName}, componentVersion: ${componentVersion}`);
const componentCachePath = getComponentCachePath(componentName, componentVersion);
const componentCachePath = getComponentCachePath(componentName, componentVersion === 'CUSTOM' ? '' : componentVersion);
debug(`componentCachePath: ${componentCachePath}`);
const lockPath = getLockFile(componentCachePath);
if (fs.existsSync(lockPath)) {
return await buildComponentInstance(componentCachePath, this.params);
}
const { zipballUrl, version } = await getZipballUrl(componentName, componentVersion);
const { zipballUrl = componentName, version = componentVersion } = await getZipballUrl(componentName, componentVersion);
debug(`zipballUrl: ${zipballUrl}`);
await download(zipballUrl, {
logger: get(this.params, 'engineLogger', get(this.params, 'logger')),
dest: componentCachePath,
filename: `${componentName}${componentVersion ? `@${componentVersion}` : ''}`,
filename: componentVersion === 'CUSTOM' ? componentName.split('/').pop()?.split('.')[0] : `${componentName}${componentVersion ? `@${componentVersion}` : ''}`,
extract: true,
headers: registry.getSignHeaders(),
});
Expand Down
16 changes: 16 additions & 0 deletions packages/load-component/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export const buildComponentInstance = async (componentPath: string, params?: any
};

export function getProvider(name: string) {
if (isValidUrl(name)) {
return [name, 'CUSTOM'];
}
assert(!includes(name, '/'), `The component name ${name} cannot contain /`);
const [componentName, componentVersion] = split(name, '@');
const { core_load_serverless_devs_component } = process.env;
Expand All @@ -84,6 +87,9 @@ export function getProvider(name: string) {
}

export const getZipballUrl = async (componentName: string, componentVersion?: string) => {
if(componentVersion === 'CUSTOM') {
return {};
}
const url = componentVersion ? getUrlWithVersion(componentName, componentVersion) : getUrlWithLatest(componentName);
debug(`url: ${url}`);
try {
Expand All @@ -102,5 +108,15 @@ export const getZipballUrl = async (componentName: string, componentVersion?: st
};

export const getComponentCachePath = (componentName: string, componentVersion?: string) => {
componentName = isValidUrl(componentName) ? componentName.split('/').pop()?.split('.')[0] || '' : componentName;
return path.join(getRootHome(), 'components', 'devsapp.cn', 'v3', componentVersion ? `${componentName}@${componentVersion}` : componentName);
};

export function isValidUrl(s: string) {
try {
new URL(s);
return true;
} catch (err) {
return false;
}
}

0 comments on commit a03d025

Please sign in to comment.