Skip to content

Commit

Permalink
feat: support angualr 17
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Nov 9, 2023
1 parent 5298fc9 commit bec4eae
Show file tree
Hide file tree
Showing 22 changed files with 247 additions and 194 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist
*.js
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-env node */
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
overrides: [
{
files: ['*.ts'],
rules: {
"@typescript-eslint/no-explicit-any": "off"
}
}
]
};
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ jobs:
with:
cmd: build

- name: lint
uses: borales/actions-yarn@v4
with:
cmd: lint

- name: test
uses: borales/actions-yarn@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12.14.1
18.13.0
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

13 changes: 13 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
singleQuote: true,
useTabs: false,
printWidth: 120,
tabWidth: 2,
semi: true,
htmlWhitespaceSensitivity: 'strict',
arrowParens: 'avoid',
bracketSpacing: true,
proseWrap: 'preserve',
trailingComma: 'none',
endOfLine: 'lf'
};
33 changes: 19 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,40 @@
"scripts": {
"test": "jest --watch",
"test:ci": "jest",
"lint": "eslint .",
"build": "tsc scripts/build.ts; node ./scripts/build.js",
"build:test": "npm run build -- --test",
"release": "npm run build -- --release"
},
"dependencies": {
"@angular-devkit/architect": "^0.1600.4",
"@angular-devkit/core": "^16.0.4",
"@angular-devkit/schematics": "^16.0.4",
"chalk": "^5.2.0",
"inquirer": "^8.0.0",
"inquirer-autocomplete-prompt": "^2.0.0",
"typescript": "~5.0.2",
"@angular-devkit/architect": "^0.1700.0",
"@angular-devkit/core": "^17.0.0",
"@angular-devkit/schematics": "^17.0.0",
"@angular-devkit/build-angular": "17.0.0",
"chalk": "^5.3.0",
"inquirer": "^9.2.12",
"inquirer-autocomplete-prompt": "^3.0.1",
"typescript": "~5.0.4",
"fs-extra": "^10.1.0",
"qiniu": "^7.8.0",
"qiniu": "^7.10.0",
"upyun": "^3.4.6",
"ali-oss": "^6.17.1"
"ali-oss": "^6.18.1"
},
"devDependencies": {
"@schematics/angular": "^16.0.4",
"@schematics/angular": "^17.0.0",
"@types/jest": "^28.1.3",
"@types/node": "^18.0.0",
"@types/inquirer": "8.2.1",
"@types/inquirer": "9.0.7",
"@types/fs-extra": "^9.0.13",
"@types/rimraf": "^3.0.2",
"@types/ali-oss": "^6.16.8",
"jest": "^28.1.1",
"ts-jest": "^28.0.5",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"prettier": "^2.8.1",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"eslint": "^8.53.0",
"@typescript-eslint/eslint-plugin": "^6.10.0",
"@typescript-eslint/parser": "^6.10.0"
},
"jest": {
"roots": [
Expand Down
13 changes: 7 additions & 6 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ const dest = (...args: string[]) => join(process.cwd(), 'dist', ...args);
const destPath = dest('');

function spawnPromise(command: string, args: string[]) {
return new Promise((resolve) => spawn(command, args, { stdio: 'inherit' }).on('close', resolve));
return new Promise(resolve => spawn(command, args, { stdio: 'inherit' }).on('close', resolve));
}

async function fixPackage() {
const path = dest('package.json');
const pkg = await import(path);
['scripts', 'devDependencies', 'jest', 'husky'].forEach((key) => delete pkg[key]);
['scripts', 'devDependencies', 'jest', 'husky'].forEach(key => delete pkg[key]);
// pkg.dependencies['@angular-devkit/architect'] = `^0.1100.0 || ^0.1200.0 || ^0.1300.0`;
// ['@angular-devkit/core', '@angular-devkit/schematics'].forEach(name => {
// pkg.dependencies[name] = `^11.0.0 || ^12.0.0 || ^13.0.0`;
// });
const rootPackage = await import(dest('../package.json'));
['@angular-devkit/architect', '@angular-devkit/core', '@angular-devkit/schematics'].forEach((key) => {
['@angular-devkit/architect', '@angular-devkit/core', '@angular-devkit/schematics'].forEach(key => {
pkg.dependencies[key] = rootPackage.dependencies[key];
});
return writeFile(path, JSON.stringify(pkg, null, 2));
Expand All @@ -36,7 +36,7 @@ async function compileSchematics() {
copy(src('builders.json'), dest('builders.json')),
copy(src('collection.json'), dest('collection.json')),
copy(src('schematics', 'schema.json'), dest('schematics', 'schema.json')),
copy(src('schematics', 'deploy', 'schema.json'), dest('schematics', 'deploy', 'schema.json')),
copy(src('schematics', 'deploy', 'schema.json'), dest('schematics', 'deploy', 'schema.json'))
]);
}

Expand All @@ -51,7 +51,7 @@ async function buildLibrary() {
if (existsSync(destPath)) {
rimraf.sync(destPath);
}
['package.json', 'README.md'].forEach((fileName) => {
['package.json', 'README.md'].forEach(fileName => {
copySync(join(process.cwd(), fileName), dest(fileName));
});
await Promise.all([compileSchematics(), await fixPackage()]);
Expand All @@ -63,7 +63,7 @@ Promise.all([buildLibrary()])
if (!TEST) {
return Promise.resolve();
}
const projectName = `ng11-strict`;
const projectName = `ng17`;
console.info(`Test mode. Copy to [${projectName}] project`);
const testProjectPath = join(process.cwd(), `../${projectName}/node_modules/ng-deploy-oss`);
if (existsSync(testProjectPath)) {
Expand All @@ -72,6 +72,7 @@ Promise.all([buildLibrary()])
return copy(destPath, testProjectPath);
})
.then(() => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const execSync = require('child_process').execSync;
const command = `cd dist & npm publish --access public --ignore-scripts`;
if (RELEASE) {
Expand Down
1 change: 0 additions & 1 deletion src/collection.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"$schema": "@angular-devkit/schematics/collection-schema.json",
"schematics": {
"ng-add": {
"description": "Adds Angular Deploy aliyun OSS, qiniu, upyun to the application without affecting any templates",
Expand Down
36 changes: 24 additions & 12 deletions src/schematics/ali-oss/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { EnvName } from '../core/types';
/**
* https://help.aliyun.com/document_detail/31837.html
* ```js
* var a = []; document.querySelectorAll('#tbody-2y5-xqs-4l8 tr').forEach(el => a.push({ name: el.querySelector('td:nth-child(1)').textContent, value: el.querySelector('td:nth-child(2)').textContent }));console.log(JSON.stringify(a));
* var a = []; document.querySelectorAll('.tbody tr').forEach(el => a.push({ name: el.querySelector('td:nth-child(1)').textContent, value: el.querySelector('td:nth-child(2)').textContent }));copy(JSON.stringify(a));
* ```
*/
export const REGIONS = [
{ name: 'Region', value: 'Region ID' },
{ name: '华东1(杭州)', value: 'oss-cn-hangzhou' },
{ name: '华东2(上海)', value: 'oss-cn-shanghai' },
{ name: '华东5(南京-本地地域)', value: 'oss-cn-nanjing' },
{ name: '华东6(福州-本地地域)', value: 'oss-cn-fuzhou' },
{ name: '华中1(武汉-本地地域)', value: 'oss-cn-wuhan' },
{ name: '华北1(青岛)', value: 'oss-cn-qingdao' },
{ name: '华北2(北京)', value: 'oss-cn-beijing' },
{ name: '华北 3(张家口)', value: 'oss-cn-zhangjiakou' },
Expand All @@ -21,20 +23,30 @@ export const REGIONS = [
{ name: '华南3(广州)', value: 'oss-cn-guangzhou' },
{ name: '西南1(成都)', value: 'oss-cn-chengdu' },
{ name: '中国香港', value: 'oss-cn-hongkong' },
{ name: '美国(硅谷)*', value: 'oss-us-west-1' },
{ name: '美国(弗吉尼亚)*', value: 'oss-us-east-1' },
{ name: '日本(东京)*', value: 'oss-ap-northeast-1' },
{ name: '美国(硅谷)', value: 'oss-us-west-1' },
{ name: '美国(弗吉尼亚)', value: 'oss-us-east-1' },
{ name: '日本(东京)', value: 'oss-ap-northeast-1' },
{ name: '韩国(首尔)', value: 'oss-ap-northeast-2' },
{ name: '新加坡*', value: 'oss-ap-southeast-1' },
{ name: '澳大利亚(悉尼)*', value: 'oss-ap-southeast-2' },
{ name: '马来西亚(吉隆坡)*', value: 'oss-ap-southeast-3' },
{ name: '印度尼西亚(雅加达)*', value: 'oss-ap-southeast-5' },
{ name: '新加坡', value: 'oss-ap-southeast-1' },
{ name: '澳大利亚(悉尼)', value: 'oss-ap-southeast-2' },
{ name: '马来西亚(吉隆坡)', value: 'oss-ap-southeast-3' },
{ name: '印度尼西亚(雅加达)', value: 'oss-ap-southeast-5' },
{ name: '菲律宾(马尼拉)', value: 'oss-ap-southeast-6' },
{ name: '泰国(曼谷)', value: 'oss-ap-southeast-7' },
{ name: '印度(孟买)*', value: 'oss-ap-south-1' },
{ name: '德国(法兰克福)*', value: 'oss-eu-central-1' },
{ name: '印度(孟买)', value: 'oss-ap-south-1' },
{ name: '德国(法兰克福)', value: 'oss-eu-central-1' },
{ name: '英国(伦敦)', value: 'oss-eu-west-1' },
{ name: '阿联酋(迪拜)*', value: 'oss-me-east-1' },
{ name: '阿联酋(迪拜)①', value: 'oss-me-east-1' },
{ name: '无地域属性(中国内地)', value: 'oss-rg-china-mainland' },
{ name: 'Region', value: 'Region ID' },
{ name: '华东1金融云', value: 'oss-cn-hzjbp' },
{ name: '华东2金融云', value: 'oss-cn-shanghai-finance-1' },
{ name: '华北2 金融云(邀测)', value: 'oss-cn-beijing-finance-1' },
{ name: '华南1金融云', value: 'oss-cn-shenzhen-finance-1' },
{ name: '杭州金融云公网', value: 'oss-cn-hzfinance' },
{ name: '上海金融云公网', value: 'oss-cn-shanghai-finance-1-pub' },
{ name: '深圳金融云公网', value: 'oss-cn-szfinance' },
{ name: '北京金融云公网', value: 'oss-cn-beijing-finance-1-pub' }
];

export const ENV_NAMES: EnvName[] = [
Expand All @@ -44,5 +56,5 @@ export const ENV_NAMES: EnvName[] = [
{ key: 'ALIOSS_STSTOKEN', name: 'stsToken' },
{ key: 'ALIOSS_BUCKET', name: 'bucket' },
{ key: 'ALIOSS_PREFIX', name: 'prefix' },
{ key: 'ALIOSS_BUILDCOMMAND', name: 'buildCommand' },
{ key: 'ALIOSS_BUILDCOMMAND', name: 'buildCommand' }
];
42 changes: 19 additions & 23 deletions src/schematics/ali-oss/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BuilderContext } from '@angular-devkit/architect';
import * as OSS from 'ali-oss';
import OSS from 'ali-oss';
import { ENV_NAMES } from './config';
import { DeployBuilderSchema } from '../core/types';
import { fixEnvValues, readFiles, uploadFiles } from '../core/utils';
Expand Down Expand Up @@ -28,7 +28,7 @@ function fixConfig(schema: AliOSSDeployBuilderSchema, context: BuilderContext) {
sk: schema.sk,
stsToken: schema.stsToken,
bucket: schema.bucket,
prefix: schema.prefix,
prefix: schema.prefix
};
context.logger.info(`📦Current configuration:`);
Object.keys(logConfog).forEach(key => {
Expand All @@ -37,25 +37,21 @@ function fixConfig(schema: AliOSSDeployBuilderSchema, context: BuilderContext) {
}

async function clear(schema: AliOSSDeployBuilderSchema, context: BuilderContext, client: OSS): Promise<void> {
return new Promise(async reslove => {
context.logger.info(`🤣 Start checking pre-deleted files`);
const resp = await client.list({ prefix: schema.prefix, 'max-keys': 1000 }, {});
if (resp.objects == null || resp.objects.length === 0) {
context.logger.info(` No need to delete files`);
reslove();
return;
}
context.logger.info(` Check that you need to delete ${resp.objects.length} files`);
const promises: Array<Promise<any>> = [];
for (const item of resp.objects) {
promises.push(client.delete(item.name));
}
if (promises.length > 0) {
await Promise.all(promises);
context.logger.info(` Successfully deleted`);
}
reslove();
});
context.logger.info(`🤣 Start checking pre-deleted files`);
const resp = await client.list({ prefix: schema.prefix, 'max-keys': 1000 }, {});
if (resp.objects == null || resp.objects.length === 0) {
context.logger.info(` No need to delete files`);
return;
}
context.logger.info(` Check that you need to delete ${resp.objects.length} files`);
const promises: Array<Promise<any>> = [];
for (const item of resp.objects) {
promises.push(client.delete(item.name));
}
if (promises.length > 0) {
await Promise.all(promises);
context.logger.info(` Successfully deleted`);
}
}

async function upload(schema: AliOSSDeployBuilderSchema, context: BuilderContext, client: OSS) {
Expand All @@ -81,14 +77,14 @@ export async function ngDeployAliOSS(schema: AliOSSDeployBuilderSchema, context:
accessKeySecret: schema.sk,
bucket: schema.bucket,
stsToken: schema.stsToken,
timeout: TIMEOUT,
timeout: TIMEOUT
});
if (schema.preClean) {
await clear(schema, context, client);
}
await upload(schema, context, client);

context.logger.warn(
`📌注意:阿里云OSS在未绑定域名的情况下直接打开 index.html 会以下载的形式出现,如何设置静态网站托管请参考:https://help.aliyun.com/document_detail/31899.html`,
`📌注意:阿里云OSS在未绑定域名的情况下直接打开 index.html 会以下载的形式出现,如何设置静态网站托管请参考:https://help.aliyun.com/document_detail/31899.html`
);
}
22 changes: 22 additions & 0 deletions src/schematics/core/load-esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Lazily compiled dynamic import loader function.
*/
let load: (<T>(modulePath: string | URL) => Promise<T>) | undefined;

/**
* This uses a dynamic import to load a module which may be ESM.
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
* will currently, unconditionally downlevel dynamic import into a require call.
* require calls cannot load ESM code and will result in a runtime error. To workaround
* this, a Function constructor is used to prevent TypeScript from changing the dynamic import.
* Once TypeScript provides support for keeping the dynamic import this workaround can
* be dropped.
*
* @param modulePath The path of the module to load.
* @returns A Promise that resolves to the dynamically imported module.
*/
export function loadEsmModule<T>(modulePath: string | URL): Promise<T> {
load ??= new Function('modulePath', `return import(modulePath);`) as Exclude<typeof load, undefined>;

return load(modulePath);
}

0 comments on commit bec4eae

Please sign in to comment.