diff --git a/packages/load-component/src/index.ts b/packages/load-component/src/index.ts index 5c94d07b..cef33dbe 100644 --- a/packages/load-component/src/index.ts +++ b/packages/load-component/src/index.ts @@ -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 ' before deploy to update your custom component cache manually.`); + return; + } debug(`componentName: ${componentName}, componentVersion: ${componentVersion}`); const componentCachePath = getComponentCachePath(componentName, componentVersion); debug(`componentCachePath: ${componentCachePath}`); @@ -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(), }); diff --git a/packages/load-component/src/utils/index.ts b/packages/load-component/src/utils/index.ts index a8f78b79..342cf188 100644 --- a/packages/load-component/src/utils/index.ts +++ b/packages/load-component/src/utils/index.ts @@ -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; @@ -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 { @@ -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; + } +}