Skip to content

Commit

Permalink
Lint files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ap3rtur3 committed Jun 18, 2024
1 parent 30d2a61 commit bb1707d
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 61 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "2.0.3",
"scripts": {
"lint": "eslint src --ext .ts --max-warnings=0",
"lint:fix": "eslint src --ext .ts --max-warnings=0 --fix",
"test": "mocha --require ts-node/register src/test/**/*.spec.ts",
"build": "tsc",
"release": "ncc build src/main/index.ts"
Expand Down
4 changes: 2 additions & 2 deletions src/main/action/files/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class Directory {
readonly relativePath: string,
readonly directories: Directory[],
readonly templateFiles: TemplateFile[],
readonly metadata: FileMetadata
readonly metadata: FileMetadata,
) {}

static openFromRepo(basePath: string, templateExtension: string): Directory {
Expand All @@ -31,7 +31,7 @@ export class Directory {
relativePath,
directories,
templateFiles,
FileMetadata.of(fs.lstatSync(path.join(basePath, relativePath)))
FileMetadata.of(fs.lstatSync(path.join(basePath, relativePath))),
);
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/action/files/fileMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import fs from 'fs';

export class FileMetadata {
constructor(readonly mode: number, readonly gid: number, readonly uid: number) {}
constructor(
readonly mode: number,
readonly gid: number,
readonly uid: number,
) {}

static of(stats: fs.Stats): FileMetadata {
return new FileMetadata(stats.mode, stats.gid, stats.uid);
Expand Down
14 changes: 11 additions & 3 deletions src/main/action/files/templateFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import fs from 'fs';
import path from 'path';

export class TemplateFile {
constructor(readonly relativePath: string, readonly content: string, readonly metadata: FileMetadata) {}
constructor(
readonly relativePath: string,
readonly content: string,
readonly metadata: FileMetadata,
) {}

static open(basePath: string, relativePath: string, templateExtension: string): TemplateFile {
if (relativePath == templateExtension || relativePath.endsWith('/' + templateExtension)) {
Expand All @@ -13,7 +17,7 @@ export class TemplateFile {
return new TemplateFile(
relativePath.replace(templateExtension, ''),
fs.readFileSync(filePath, { encoding: 'utf8', flag: 'r' }),
FileMetadata.of(fs.lstatSync(filePath))
FileMetadata.of(fs.lstatSync(filePath)),
);
}

Expand All @@ -23,7 +27,11 @@ export class TemplateFile {
}

export class RenderedTemplateFile {
constructor(readonly relativePath: string, readonly content: string, readonly metadata: FileMetadata) {}
constructor(
readonly relativePath: string,
readonly content: string,
readonly metadata: FileMetadata,
) {}

updateFile(basePath: string) {
fs.writeFileSync(path.join(basePath, this.relativePath), this.content);
Expand Down
12 changes: 6 additions & 6 deletions src/main/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function renderTemplates(configuration: Configuration): Promise<Rec
const intermediateDataFile = IntermediateDataFile.readOrDefault(configuration.intermediateDataFile);
const referenceProvider = new ReferenceProvider(
intermediateDataFile.getReferencesMap(),
configuration.useContextSensitiveReferences ? generateContextSensitiveReference : undefined
configuration.useContextSensitiveReferences ? generateContextSensitiveReference : undefined,
);
const dataProvider = new DataProvider(isDataSet ? Data.fromInput(configuration.deploymentId) : undefined);
const templateEngine = new TemplateEngine(referenceProvider, dataProvider);
Expand All @@ -30,7 +30,7 @@ export async function renderTemplates(configuration: Configuration): Promise<Rec
insertReferences(configuration, templateEngine, referenceProvider);
} else {
throw new Error(
'At least one of the input properties "data" and "intermediate-data-file" need to be supplied. Additionally you could supply a "deployment-id" to use deployment specific values'
'At least one of the input properties "data" and "intermediate-data-file" need to be supplied. Additionally you could supply a "deployment-id" to use deployment specific values',
);
}
}
Expand All @@ -39,7 +39,7 @@ export async function renderTemplates(configuration: Configuration): Promise<Rec

function renderWithoutReferences(
{ inputDirectory, templateExtension, outputDirectory }: Configuration,
templateEngine: TemplateEngine
templateEngine: TemplateEngine,
) {
const renderer = templateEngine.newRenderer().parse('default').steps('replaceData').render;

Expand All @@ -53,7 +53,7 @@ function renderWithoutReferences(
function renderWithReferences(
{ inputDirectory, outputDirectory, templateExtension }: Configuration,
templateEngine: TemplateEngine,
intermediateDataFile: IntermediateDataFile
intermediateDataFile: IntermediateDataFile,
) {
if (inputDirectory !== outputDirectory) {
throw new Error('"input-directory" and "output-directory" should be the same when completing the templating');
Expand All @@ -62,14 +62,14 @@ function renderWithReferences(
const renderer = templateEngine.newRenderer().parse('references').steps('resolveReferences', 'replaceData').render;

intermediateDataFile.forEachCreatedFile((filePath) =>
TemplateFile.open(inputDirectory, filePath, templateExtension).renderWith(renderer).updateFile(outputDirectory)
TemplateFile.open(inputDirectory, filePath, templateExtension).renderWith(renderer).updateFile(outputDirectory),
);
}

function insertReferences(
{ inputDirectory, outputDirectory, templateExtension, intermediateDataFile }: Required<Configuration>,
templateEngine: TemplateEngine,
referenceProvider: ReferenceProvider
referenceProvider: ReferenceProvider,
) {
const renderer = templateEngine.newRenderer().parse('default').steps('insertReferences').render;

Expand Down
2 changes: 1 addition & 1 deletion src/main/action/input/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class Data extends Map<string, string> {
return data;
} catch (e) {
throw new Error(
'Property "data" was not supplied properly. Please add "data: \'[ ${{ toJson(secrets) }}, ${{ steps.deployment.outputs.template-variables }} ]\'" to the "with" section of this action.'
'Property "data" was not supplied properly. Please add "data: \'[ ${{ toJson(secrets) }}, ${{ steps.deployment.outputs.template-variables }} ]\'" to the "with" section of this action.',
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/engine/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const insertReferences = (referenceProvider: ReferenceProvider) => (literal) =>
return new Literal(referenceProvider.createExpressionReference(literal.value), LiteralType.String);
case LiteralType.Reference:
throw new Error(
'References are not supported in this mode. This should not happen, please contact the deploy-now team.'
'References are not supported in this mode. This should not happen, please contact the deploy-now team.',
);
}
};
Expand All @@ -30,7 +30,7 @@ const resolveReferences = (referenceProvider: ReferenceProvider) => (literal) =>
return new Literal(referenceValue.expression, LiteralType.Expression);
}
throw new Error(
`Could not resolve reference ${literal.value}. This should not happen, please contact the deploy-now team.`
`Could not resolve reference ${literal.value}. This should not happen, please contact the deploy-now team.`,
);
}
return literal;
Expand All @@ -46,7 +46,7 @@ const replaceData = (dataProvider: DataProvider) => (literal) => {
return new Literal(evaluateExpression(literal.value, dataProvider), LiteralType.String);
case LiteralType.Reference:
throw new Error(
'References are not supported in this mode. This should not happen, please contact the deploy-now team.'
'References are not supported in this mode. This should not happen, please contact the deploy-now team.',
);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/main/engine/dataProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default class DataProvider {
constructor(
private readonly data: Map<string, string> | undefined = undefined,
private readonly defaultAction: (key: string) => string = (key) => '$' + key
private readonly defaultAction: (key: string) => string = (key) => '$' + key,
) {}

has(key: string): boolean {
Expand Down
7 changes: 5 additions & 2 deletions src/main/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export default class TemplateEngine {
private readonly parserModes: Record<'default' | 'references', Parser>;
private readonly converters: Record<'resolveReferences' | 'insertReferences' | 'replaceData', Converter>;

constructor(referenceProvider: ReferenceProvider, private dataProvider: DataProvider) {
constructor(
referenceProvider: ReferenceProvider,
private dataProvider: DataProvider,
) {
this.parserModes = {
default: new DefaultParser(),
references: new IntermediateParser([...referenceProvider.knownReferences.keys()]),
Expand All @@ -30,7 +33,7 @@ export default class TemplateEngine {
const result = parser.parse(content).map((value) => steps.reduce((literal, step) => step(literal), value));
if (result.some((value) => value.type != LiteralType.String)) {
throw new Error(
'Not all literals were converted during rendering. This should not happen, please contact the deploy-now team.'
'Not all literals were converted during rendering. This should not happen, please contact the deploy-now team.',
);
}
return result.map((value) => value.value).join('');
Expand Down
5 changes: 4 additions & 1 deletion src/main/engine/literal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export class Literal {
constructor(readonly value: string, readonly type: LiteralType) {}
constructor(
readonly value: string,
readonly type: LiteralType,
) {}
}

export enum LiteralType {
Expand Down
4 changes: 2 additions & 2 deletions src/main/engine/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class DefaultParser extends Parser {
super(
new RegExp(
'([$][A-Za-z_][A-Za-z0-9_]*)|([$][{] *[A-Za-z_][A-Za-z0-9_]*([.][A-Za-z_][A-Za-z0-9_]*[(][)])? *[}])',
'g'
)
'g',
),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/engine/referenceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class ReferenceProvider {

constructor(
readonly existingReferences: Map<string, ReferenceValue>,
private keySupplier: (originalValue: string) => string = uuid.v4()
private keySupplier: (originalValue: string) => string = uuid.v4(),
) {}

createKeyReference(value: string): string {
Expand Down
2 changes: 1 addition & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ Action.run<Configuration, Record<string, never>>(
intermediateDataFile: input.optional('intermediate-data-file'),
templateExtension: input.required('template-extension'),
useContextSensitiveReferences: input.required('use-context-sensitive-references') === 'true',
}
},
);
16 changes: 8 additions & 8 deletions src/test/action/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('Test main action function', () => {
['IONOS_MAIL_PASSWORD', 'mail-password'],
['IONOS_DB_USER', 'db-user'],
['IONOS_DB_PASSWORD', 'db-password'],
])
]),
);

await renderTemplates({
Expand All @@ -58,7 +58,7 @@ describe('Test main action function', () => {
'030662b7-d260-4c51-855f-19847ec9ac36',
'14588eb2-e5e0-42a9-a4fd-7e9a5a0c435a',
'caf2ba9d-de63-4b31-b3fa-7b69a5158988',
'9acd4997-c4b6-44fe-b7be-17f7d9e33881'
'9acd4997-c4b6-44fe-b7be-17f7d9e33881',
);

await renderTemplates({
Expand All @@ -84,7 +84,7 @@ describe('Test main action function', () => {
['IONOS_MAIL_PASSWORD', 'mail-password'],
['IONOS_DB_USER', 'db-user'],
['IONOS_DB_PASSWORD', 'db-password'],
])
]),
);

await renderTemplates({
Expand All @@ -104,7 +104,7 @@ describe('Test main action function', () => {
'030662b7-d260-4c51-855f-19847ec9ac36',
'14588eb2-e5e0-42a9-a4fd-7e9a5a0c435a',
'caf2ba9d-de63-4b31-b3fa-7b69a5158988',
'9acd4997-c4b6-44fe-b7be-17f7d9e33881'
'9acd4997-c4b6-44fe-b7be-17f7d9e33881',
);

await renderTemplates({
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('Test main action function', () => {
templateExtension: '.template',
useContextSensitiveReferences: true,
}).should.be.rejectedWith(
'At least one of the input properties "data" and "intermediate-data-file" need to be supplied. Additionally you could supply a "deployment-id" to use deployment specific values'
'At least one of the input properties "data" and "intermediate-data-file" need to be supplied. Additionally you could supply a "deployment-id" to use deployment specific values',
);
});

Expand All @@ -163,7 +163,7 @@ describe('Test main action function', () => {
templateExtension: '.template',
useContextSensitiveReferences: true,
}).should.be.rejectedWith(
'"input-directory" and "output-directory" should be the same when completing the templating'
'"input-directory" and "output-directory" should be the same when completing the templating',
);
});

Expand Down Expand Up @@ -222,7 +222,7 @@ describe('Test main action function', () => {
['IONOS_MAIL_PASSWORD', 'mail-password'],
['IONOS_DB_USER', 'db-user'],
['IONOS_DB_PASSWORD', 'db-password'],
])
]),
);

await renderTemplates({
Expand Down Expand Up @@ -252,7 +252,7 @@ function withFixedReferences(...references: string[]) {
sinon.replace(
referenceGenerator,
'generateContextSensitiveReference',
() => reverseReferences.pop() || assert.fail('More references used than expected')
() => reverseReferences.pop() || assert.fail('More references used than expected'),
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/action/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ describe('Test data extraction from input', () => {
['IONOS_DB_PASSWORD', 'db-password'],
['IONOS_MAIL_USER', 'mail-user'],
['IONOS_MAIL_PASSWORD', 'mail-password'],
])
]),
);
});

it('Show error, if data was empty', () => {
getInput.returns('');

expect(() => Data.fromInput('ff3f20ab-5cd4-49c8-8424-fef76d766229')).to.throw(
'Property "data" was not supplied properly. Please add "data: \'[ ${{ toJson(secrets) }}, ${{ steps.deployment.outputs.template-variables }} ]\'" to the "with" section of this action.'
'Property "data" was not supplied properly. Please add "data: \'[ ${{ toJson(secrets) }}, ${{ steps.deployment.outputs.template-variables }} ]\'" to the "with" section of this action.',
);
});

Expand All @@ -62,7 +62,7 @@ describe('Test data extraction from input', () => {
new Map<string, string>([
['IONOS_MAIL_USER', 'mail-user'],
['IONOS_MAIL_PASSWORD', 'mail-password'],
])
]),
);
});

Expand All @@ -84,7 +84,7 @@ describe('Test data extraction from input', () => {
['IONOS_DB_PASSWORD', 'db-password'],
['IONOS_MAIL_USER', 'mail-user'],
['IONOS_MAIL_PASSWORD', 'mail-password'],
])
]),
);
});

Expand All @@ -110,7 +110,7 @@ describe('Test data extraction from input', () => {
['IONOS_MAIL_USER', 'mail-user'],
['IONOS_MAIL_PASSWORD', 'mail-password'],
['APP_URL', 'app-url'],
])
]),
);
});
});
2 changes: 1 addition & 1 deletion src/test/action/intermediateDataFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('Test interactions with intermediate data file', () => {
['b486c8a2-501f-48b5-b1bf-2c3c75ccaf87', { key: 'ABC' }],
['https://ff3f20ab-5cd4-49c8-8424-fef76d766229', { key: 'BCD' }],
['50000', { key: 'CDE' }],
])
]),
);
});
});
Loading

0 comments on commit bb1707d

Please sign in to comment.