Skip to content

Commit

Permalink
feat: update download url of Dockerfiles if available
Browse files Browse the repository at this point in the history
  • Loading branch information
kyubisation committed Oct 25, 2020
1 parent 1277713 commit 368e5eb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Usage: ngssc insert [options] [directory]
Dockerfile
```Dockerfile
FROM nginx:alpine
ADD https://github.com/kyubisation/angular-server-side-configuration/releases/download/v9.0.1/ngssc_64bit /usr/sbin/ngssc
ADD https://github.com/kyubisation/angular-server-side-configuration/releases/download/v0.0.0/ngssc_64bit /usr/sbin/ngssc
RUN chmod +x /usr/sbin/ngssc
COPY dist /usr/share/nginx/html
COPY start.sh start.sh
Expand Down
5 changes: 5 additions & 0 deletions schematics/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"version": "9-next",
"description": "Updates angular-server-side-configuration to v9",
"factory": "./ng-update/index#updateToV9"
},
"dockerfile": {
"version": "replaced by prepack",
"description": "Updates the download url for ngssc",
"factory": "./ng-update/index#dockerfile"
}
}
}
2 changes: 1 addition & 1 deletion schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { join } from 'path';
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
version: '10.0.0',
};

const appOptions: ApplicationOptions = {
Expand Down
21 changes: 20 additions & 1 deletion schematics/ng-update/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { join } from 'path';
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '9.0.0-rc.11',
version: '10.0.0',
};

const appOptions: ApplicationOptions = {
Expand Down Expand Up @@ -160,4 +160,23 @@ describe('ng-update', () => {
expect(migratedWorkspace.projects.dummy.architect!.ngsscbuild.configurations.production)
.not.toHaveProperty('aotSupport');
});

it('should update Dockerfile', async () => {
appTree.create('Dockerfile', `
FROM nginx:alpine
ADD https://github.com/kyubisation/angular-server-side-configuration/releases/download/v9.0.1/ngssc_64bit /usr/sbin/ngssc
RUN chmod +x /usr/sbin/ngssc
COPY dist /usr/share/nginx/html
COPY start.sh start.sh
RUN chmod +x ./start.sh
CMD ["./start.sh"]
`);
const tree = await runner
.runSchematicAsync('dockerfile', {}, appTree)
.toPromise();

const dockerfileContent = tree.read('Dockerfile')!.toString();
const version = require('../../package.json').version;
expect(dockerfileContent).toContain(`https://github.com/kyubisation/angular-server-side-configuration/releases/download/v${version}/ngssc_64bit`);
});
});
19 changes: 18 additions & 1 deletion schematics/ng-update/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Path } from '@angular-devkit/core';
import { basename, Path } from '@angular-devkit/core';
import { chain, FileEntry, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { getWorkspace, updateWorkspace } from '@schematics/angular/utility/config';

Expand Down Expand Up @@ -50,6 +50,23 @@ export function updateToV9(): Rule {
};
}

export function dockerfile(): Rule {
return (tree: Tree) => {
const downloadUrlRegex = /https:\/\/github.com\/kyubisation\/angular-server-side-configuration\/releases\/download\/v((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)/;
const version = require('../../package.json').version;
tree.visit((path, entry) => {
if (basename(path).indexOf('Dockerfile') >= 0 &&entry && entry.content.toString().match(downloadUrlRegex)) {
const content = entry.content
.toString()
.replace(
new RegExp(downloadUrlRegex.source, 'g'),
`https://github.com/kyubisation/angular-server-side-configuration/releases/download/v${version}`);
tree.overwrite(path, content);
}
});
};
}

function tryReadNgsscJson(tree: Tree): Partial<Ngssc> {
const ngssc = tree.read(NGSSC_JSON_PATH);
return ngssc ? JSON.parse(ngssc.toString('utf8')) : {};
Expand Down
17 changes: 17 additions & 0 deletions scripts/patch-dockerfile-migration-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { writeFileSync, readFileSync } = require('fs');
const { join } = require('path');

if (require.main === module) {
const version = process.env.npm_package_version || require('../package.json').version;

const migrationJsonPath = '../schematics/migration.json';
const migrationJson = require(migrationJsonPath);
migrationJson.schematics.dockerfile.version = version;
writeFileSync(join(__dirname, migrationJsonPath), JSON.stringify(migrationJson, null, 2), 'utf8');

const readmePath = join(__dirname, '../README.md');
const readmeContent = readFileSync(readmePath, 'utf8').replace(
/https:\/\/github.com\/kyubisation\/angular-server-side-configuration\/releases\/download\/v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/g,
`https://github.com/kyubisation/angular-server-side-configuration/releases/download/v${version}`);
writeFileSync(readmePath, readmeContent, 'utf8');
}

0 comments on commit 368e5eb

Please sign in to comment.