Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d826b39
chore: update formatter conf and apply formatter
pranjalwalia Jul 6, 2022
0d172c8
chore: update nodecloud conf
pranjalwalia Jul 6, 2022
6e40593
feat: add SDK in workspace generator
pranjalwalia Jul 6, 2022
260bd55
feat: add parser for service aliyun
pranjalwalia Jul 6, 2022
0e6e998
feat: update parser consumption
pranjalwalia Jul 6, 2022
83d0048
tests: add tests for the aliyun parser
pranjalwalia Jul 6, 2022
11fa9bc
chore: update main file
pranjalwalia Jul 6, 2022
eed5587
chore: update formatter spacing and apply
pranjalwalia Jul 9, 2022
0030f25
Merge pull request #137 from masterchief01/formatter-commit
rajitha1998 Jul 16, 2022
5c80a22
Merge branch 'generator-ali' of github.com:masterchief01/nodecloud in…
pranjalwalia Jul 16, 2022
150374c
chore: run formatter on remaining files
pranjalwalia Jul 16, 2022
afaa449
chore: addition of transformations to generator
pranjalwalia Jul 16, 2022
856345b
chore: removal of redundant identifiers
pranjalwalia Jul 16, 2022
cb492ea
chore: update tests transformer with dummy class
pranjalwalia Jul 16, 2022
a9f6de8
chore: update annotation for ali class dummyFile
pranjalwalia Jul 22, 2022
20382ea
chore: generate transpiled output, format tests
pranjalwalia Jul 22, 2022
097ed8c
Merge pull request #138 from masterchief01/aliyun-parser
rajitha1998 Jul 23, 2022
f24d975
Merge branch 'generator-ali' of github.com:masterchief01/nodecloud in…
pranjalwalia Jul 29, 2022
cacfa2f
Merge pull request #139 from masterchief01/aliyun-transformer
rajitha1998 Aug 4, 2022
b302e56
feat: add nodecloud plugin starter for alicloud
pranjalwalia Aug 12, 2022
7830745
chore: update env credential checks
pranjalwalia Aug 12, 2022
a41c193
Merge pull request #140 from masterchief01/package-workspace
rajitha1998 Aug 20, 2022
14a45ea
feat: update ali-cloud plugin
pranjalwalia Aug 29, 2022
16d411b
docs: update docs for nodecloud and generator
pranjalwalia Sep 3, 2022
ab516eb
feat: add plugin usage examples
pranjalwalia Sep 3, 2022
bfac1b1
Merge pull request #141 from masterchief01/package-services
rajitha1998 Sep 8, 2022
fd66534
Merge pull request #143 from masterchief01/generator-docs
rajitha1998 Sep 8, 2022
6f7a8ac
Merge pull request #144 from masterchief01/plugin-usage
rajitha1998 Sep 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: run formatter on remaining files
  • Loading branch information
pranjalwalia committed Jul 16, 2022
commit 150374c8c858facd000b9bdb4e940f294c5f4cbd
37 changes: 17 additions & 20 deletions generator/generators/aliyun/generator.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { ClassData, extractSDKData, getAST } from '../../parsers/aliyun/parser';

export const generateAliyunClass = (
serviceClass: unknown,
serviceName: string
serviceClass: unknown,
serviceName: string
) => {
const sdkfile = serviceClass[Object.keys(serviceClass)[0]];
getAST(sdkfile)
.then(async result => {
const sdkClassAst = result;
try {
const classData: ClassData = extractSDKData(
sdkClassAst,
serviceClass
);
classData.serviceName = serviceName;
} catch (err) {
console.error('Error : ', err);
}
})
.catch(error => {
console.error('Error : ', error);
});
};
const sdkfile = serviceClass[Object.keys(serviceClass)[0]];
getAST(sdkfile)
.then(async result => {
const sdkClassAst = result;
try {
const classData: ClassData = extractSDKData(sdkClassAst, serviceClass);
classData.serviceName = serviceName;
} catch (err) {
console.error('Error : ', err);
}
})
.catch(error => {
console.error('Error : ', error);
});
};
197 changes: 97 additions & 100 deletions generator/parsers/aliyun/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,112 @@ import * as path from 'path';
import { createSourceFile, ScriptTarget, SyntaxKind } from 'typescript';

export const getAST = (sdkFilePath: string) => {
return new Promise(async (resolve, reject) => {
const [module, rootFile, service] = sdkFilePath.split(' ');
try {
const file = path.join(
__dirname,
'../../../node_modules/aliyun-v2-typescript-sdk/dist/modules/',
module.toLowerCase(),
rootFile
);

const ast = createSourceFile(
file,
fs.readFileSync(file).toString(),
ScriptTarget.Latest,
true
);

let cloned = null;

await ast.forEachChild(child => {
if (SyntaxKind[child.kind] === 'ClassDeclaration') {
cloned = Object.assign({}, child);
}
});

if (!cloned) {
reject(new Error('Class not found!'));
} else {
resolve(cloned);
}
} catch (error) {
if (error.code === 'ENOENT') {
reject(new Error('File not found!'));
} else {
reject(error);
}
}
});
return new Promise(async (resolve, reject) => {
const [module, rootFile, service] = sdkFilePath.split(' ');
try {
const file = path.join(
__dirname,
'../../../node_modules/aliyun-v2-typescript-sdk/dist/modules/',
module.toLowerCase(),
rootFile
);

const ast = createSourceFile(
file,
fs.readFileSync(file).toString(),
ScriptTarget.Latest,
true
);

let cloned = null;

await ast.forEachChild(child => {
if (SyntaxKind[child.kind] === 'ClassDeclaration') {
cloned = Object.assign({}, child);
}
});

if (!cloned) {
reject(new Error('Class not found!'));
} else {
resolve(cloned);
}
} catch (error) {
if (error.code === 'ENOENT') {
reject(new Error('File not found!'));
} else {
reject(error);
}
}
});
};

export const extractSDKData = (sdkClassAst, serviceClass): ClassData => {
const methods: FunctionData[] = [];
const functions = [];

Object.keys(serviceClass).forEach((key: string) => {
functions.push(serviceClass[key].split(' ')[2]);
});

sdkClassAst.members.forEach(method => {
if (method.name && functions.includes(method.name.text)) {
let name;
Object.keys(serviceClass).forEach((key: string) => {
if (serviceClass[key].split(' ')[2] === method.name.text) {
name = key;
}
});

const parameters = [];
method.parameters.forEach(param => {
if (param.name.text !== 'callback') {
const parameter = {
name: param.name.text,
optional: param.questionToken ? true : false,
type: SyntaxKind[param.type.kind],
typeName: null,
};

if (
parameter.type === 'TypeReference' &&
param.type.typeName
) {
parameter.typeName = param.type.typeName.text;
}

parameters.push(parameter);
}
});

methods.push({
functionName: name.toString(),
SDKFunctionName: method.name.text.toString(),
params: parameters,
});
}
});

const classData: ClassData = {
className: sdkClassAst.name.text,
functions: methods,
serviceName: null,
};

return classData;
const methods: FunctionData[] = [];
const functions = [];

Object.keys(serviceClass).forEach((key: string) => {
functions.push(serviceClass[key].split(' ')[2]);
});

sdkClassAst.members.forEach(method => {
if (method.name && functions.includes(method.name.text)) {
let name;
Object.keys(serviceClass).forEach((key: string) => {
if (serviceClass[key].split(' ')[2] === method.name.text) {
name = key;
}
});

const parameters = [];
method.parameters.forEach(param => {
if (param.name.text !== 'callback') {
const parameter = {
name: param.name.text,
optional: param.questionToken ? true : false,
type: SyntaxKind[param.type.kind],
typeName: null,
};

if (parameter.type === 'TypeReference' && param.type.typeName) {
parameter.typeName = param.type.typeName.text;
}

parameters.push(parameter);
}
});

methods.push({
functionName: name.toString(),
SDKFunctionName: method.name.text.toString(),
params: parameters,
});
}
});

const classData: ClassData = {
className: sdkClassAst.name.text,
functions: methods,
serviceName: null,
};

return classData;
};

export interface ClassData {
className: string;
functions: FunctionData[];
serviceName: string;
className: string;
functions: FunctionData[];
serviceName: string;
}

interface FunctionData {
functionName: string;
SDKFunctionName: string;
params: param[];
functionName: string;
SDKFunctionName: string;
params: param[];
}

interface param {
name: string;
type: string;
typeName: string;
}
name: string;
type: string;
typeName: string;
}
38 changes: 19 additions & 19 deletions generator/test/parsers/aliyun/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import { SyntaxKind } from 'typescript';
import { getAST } from '../../../parsers/aliyun/parser';

describe('Aliyun getAST Implementation', () => {
const sdkFilePath = 'oss index.d.ts setRegion';
const invalidPath = 'oss unknown.d.ts setRegion';
context('With existing file', () => {
it('Should return Abstract syntax tree of the class', async () => {
const ast: any = await getAST(sdkFilePath);
expect(ast).to.be.an('object');
expect(SyntaxKind[ast.kind] === 'ClassDeclaration').to.be.true;
});
});
const sdkFilePath = 'oss index.d.ts setRegion';
const invalidPath = 'oss unknown.d.ts setRegion';
context('With existing file', () => {
it('Should return Abstract syntax tree of the class', async () => {
const ast: any = await getAST(sdkFilePath);
expect(ast).to.be.an('object');
expect(SyntaxKind[ast.kind] === 'ClassDeclaration').to.be.true;
});
});

context('With non-existing file', () => {
it('should return File not found Error', async () => {
try {
await getAST(invalidPath);
} catch (error) {
expect(error.message).to.eql('File not found!');
}
});
});
});
context('With non-existing file', () => {
it('should return File not found Error', async () => {
try {
await getAST(invalidPath);
} catch (error) {
expect(error.message).to.eql('File not found!');
}
});
});
});