Skip to content

Commit 4e1041e

Browse files
Merge pull request #124 from ngx-builders/feat/update-package-and-provide-withDeps-flag
Feat/update package and provide with deps flag
2 parents 9601b4a + ddf13ab commit 4e1041e

File tree

5 files changed

+140
-82
lines changed

5 files changed

+140
-82
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
working-directory: ./src
1919
strategy:
2020
matrix:
21-
node-version: [12.x]
21+
node-version: [14.x]
2222

2323
steps:
2424
- uses: actions/checkout@v2

src/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ This command causes the `--configuration` setting to have no effect.
7171

7272
Specifies the base URL for the application being built.
7373
Same as `ng build --base-href=/XXX/`
74+
75+
#### --with-deps <a name="withDeps"></a>
76+
- **optional**
77+
- Default: `false` (string)
78+
- Example:
79+
- `ng deploy --with-deps` – Use this flag with Nx, to build your app withDeps.
7480
## License
7581

7682
MIT

src/deploy/index.ts

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,92 @@
1-
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
2-
import { json } from '@angular-devkit/core';
3-
import { Schema } from './schema';
4-
const NetlifyAPI = require('netlify');
1+
import {
2+
BuilderContext,
3+
BuilderOutput,
4+
createBuilder,
5+
} from "@angular-devkit/architect";
6+
import { json } from "@angular-devkit/core";
7+
import { Schema } from "./schema";
8+
const NetlifyAPI = require("netlify");
59

610
export default createBuilder(
7-
async (builderConfig: Schema, context: BuilderContext): Promise<BuilderOutput> => {
11+
async (
12+
builderConfig: Schema,
13+
context: BuilderContext
14+
): Promise<BuilderOutput> => {
815
context.reportStatus(`Executing deploy...`);
916
context.logger.info(`Executing netlify deploy command ...... `);
1017

1118
if (builderConfig.noBuild) {
1219
context.logger.info(`📦 Skipping build`);
1320
} else {
14-
const configuration = builderConfig.configuration || 'production';
21+
const configuration = builderConfig.configuration || "production";
1522

16-
const overrides = {
23+
const withDeps = {
24+
...{ withDeps: builderConfig.withDeps },
25+
};
26+
27+
let overrides: any = {
1728
// this is an example how to override the workspace set of options
18-
...(builderConfig.baseHref && { baseHref: builderConfig.baseHref })
29+
...(builderConfig.baseHref && {
30+
baseHref: builderConfig.baseHref,
31+
}),
1932
};
2033

21-
if (!context.target) {
22-
throw new Error('Cannot build the application without a target');
34+
if (builderConfig.withDeps) {
35+
overrides = {
36+
...(builderConfig.baseHref && {
37+
baseHref: builderConfig.baseHref,
38+
}),
39+
withDeps: builderConfig.withDeps,
40+
};
2341
}
2442

25-
const baseHref = builderConfig.baseHref ? `Your base-href: "${builderConfig.baseHref}` : '';
26-
const buildTarget = builderConfig.buildTarget ? builderConfig.buildTarget : 'build';
27-
context.logger.info(`📦 Building "${context.target.project}". Configuration: "${configuration}". Build Command: ${buildTarget}. ${baseHref}`);
43+
if (!context.target) {
44+
throw new Error(
45+
"Cannot build the application without a target"
46+
);
47+
}
2848

29-
const build = await context.scheduleTarget({
30-
target: buildTarget,
31-
project: context.target.project || '',
32-
configuration
33-
}, overrides as json.JsonObject);
49+
const baseHref = builderConfig.baseHref
50+
? `Your base-href: "${builderConfig.baseHref}`
51+
: "";
52+
const buildTarget = builderConfig.buildTarget
53+
? builderConfig.buildTarget
54+
: "build";
55+
context.logger.info(
56+
`📦 Building "${context.target.project}". Configuration: "${configuration}". Build Command: ${buildTarget}. ${baseHref}`
57+
);
58+
59+
const build = await context.scheduleTarget(
60+
{
61+
target: buildTarget,
62+
project: context.target.project || "",
63+
configuration,
64+
},
65+
overrides as json.JsonObject
66+
);
3467

3568
const buildResult = await build.result;
3669

3770
if (buildResult.success !== true) {
3871
context.logger.error(`❌ Application build failed`);
3972
return {
4073
error: `❌ Application build failed`,
41-
success: false
74+
success: false,
4275
};
4376
}
4477

4578
context.logger.info(`✔ Build Completed`);
4679
}
4780

48-
const netlifyToken = process.env.NETLIFY_TOKEN || builderConfig.netlifyToken;
49-
if (netlifyToken === '' || netlifyToken === undefined) {
81+
const netlifyToken =
82+
process.env.NETLIFY_TOKEN || builderConfig.netlifyToken;
83+
if (netlifyToken === "" || netlifyToken === undefined) {
5084
context.logger.error("🚨 Netlify Token not found !");
5185
return { success: false };
5286
}
5387

5488
let siteId = process.env.NETLIFY_API_ID || builderConfig.siteId;
55-
if (siteId === '' || siteId === undefined) {
89+
if (siteId === "" || siteId === undefined) {
5690
// site id is needed if the create option is false
5791
if (builderConfig.create === false) {
5892
context.logger.error("🚨 API ID (Site ID) not found !");
@@ -61,11 +95,11 @@ export default createBuilder(
6195
}
6296

6397
const client = new NetlifyAPI(netlifyToken, {
64-
userAgent: 'netlify/js-client',
65-
scheme: 'https',
66-
host: 'api.netlify.com',
67-
pathPrefix: '/api/v1',
68-
globalParams: {}
98+
userAgent: "netlify/js-client",
99+
scheme: "https",
100+
host: "api.netlify.com",
101+
pathPrefix: "/api/v1",
102+
globalParams: {},
69103
});
70104

71105
// let check if the site exists
@@ -82,20 +116,20 @@ export default createBuilder(
82116
// if the create is false - just return the error
83117
if (builderConfig.create !== true) {
84118
return {
85-
success: false
119+
success: false,
86120
};
87121
}
88122
break;
89123
case 401:
90124
context.logger.fatal("🚨 Netlify: Unauthorized Token");
91125
return {
92-
success: false
126+
success: false,
93127
};
94128
default:
95-
// for all other errors
129+
// for all other errors
96130
return {
97131
error: e.message,
98-
success: false
132+
success: false,
99133
};
100134
}
101135
}
@@ -106,36 +140,48 @@ export default createBuilder(
106140
context.logger.info(`Creating new site for the application`);
107141
site = await client.createSite();
108142
siteId = site.id as string;
109-
context.logger.info(`✔ Site "${site.name}" (${siteId}) created. Please update the angular.json so on the next run we can re-deploy on the same site`);
143+
context.logger.info(
144+
`✔ Site "${site.name}" (${siteId}) created. Please update the angular.json so on the next run we can re-deploy on the same site`
145+
);
110146
} catch (e) {
111147
context.logger.error("🚨 Unable to create the site");
112148
return {
113149
error: e.message,
114-
success: false
150+
success: false,
115151
};
116152
}
117153
}
118154

119155
// if we still don't have the site return with error
120156
if (!site) {
121-
context.logger.error("🚨 Unable to deploy as we don't have any context about the site");
157+
context.logger.error(
158+
"🚨 Unable to deploy as we don't have any context about the site"
159+
);
122160
return {
123161
error: "🚨 Unable to deploy as we don't have any context about the site",
124-
success: false
162+
success: false,
125163
};
126164
}
127165

128166
// lets deploy the application to the site
129167
try {
130-
context.logger.info(`Deploying project from 📂 ./${builderConfig.outputPath}`);
131-
const response = await client.deploy(siteId, builderConfig.outputPath);
132-
context.logger.info(`✔ Your updated site 🕸 is running at ${response.deploy.ssl_url}`);
168+
context.logger.info(
169+
`Deploying project from 📂 ./${builderConfig.outputPath}`
170+
);
171+
const response = await client.deploy(
172+
siteId,
173+
builderConfig.outputPath
174+
);
175+
context.logger.info(
176+
`✔ Your updated site 🕸 is running at ${response.deploy.ssl_url}`
177+
);
133178
return { success: true };
134179
} catch (e) {
135180
context.logger.error(`❌ Deployment failed: ${e.message}`);
136181
return {
137182
error: e.message,
138-
success: false
183+
success: false,
139184
};
140185
}
141-
});
186+
}
187+
);

src/deploy/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export interface Schema {
77
siteId?: string;
88
baseHref?: string;
99
create?: boolean;
10+
withDeps?: boolean
1011
}

src/deploy/schema.json

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
11
{
2-
"$id": "Schema",
3-
"title": "schema",
4-
"description": "Deployment of Angular CLI applications to Netlify",
5-
"properties": {
6-
"buildTarget": {
7-
"type": "string",
8-
"default": "build",
9-
"description": "A build target command."
10-
},
11-
"configuration": {
12-
"type": "string",
13-
"default": "production",
14-
"description": "A named build target, as specified in the `configurations` section of angular.json. Each named target is accompanied by a configuration of option defaults for that target. Same as `ng build --configuration=XXX`.",
15-
"alias": "c"
16-
},
17-
"noBuild": {
18-
"type": "boolean",
19-
"default": false,
20-
"description": "Skip build process during deployment."
21-
},
22-
"outputPath": {
23-
"type": "string",
24-
"description": "This is one of the options you can freely choose according to your needs. --- We will 'deploy' to this folder."
25-
},
26-
"netlifyToken": {
27-
"type": "string",
28-
"description": "Acess token give you the ability to communicate with netlify over API."
29-
},
30-
"siteId": {
31-
"type": "string",
32-
"description": "Every netlify app have a API ID which uniquly identify that app."
33-
},
34-
"baseHref": {
35-
"type": "string",
36-
"description": "This is an example how to override the workspace set of options. --- Base url for the application being built. Same as `ng build --base-href=/XXX/`."
37-
},
38-
"create": {
39-
"type": "boolean",
40-
"default": false,
41-
"description": "Creates the site if it does not exists or no site id is set"
2+
"$id": "Schema",
3+
"title": "schema",
4+
"description": "Deployment of Angular CLI applications to Netlify",
5+
"properties": {
6+
"buildTarget": {
7+
"type": "string",
8+
"default": "build",
9+
"description": "A build target command."
10+
},
11+
"configuration": {
12+
"type": "string",
13+
"default": "production",
14+
"description": "A named build target, as specified in the `configurations` section of angular.json. Each named target is accompanied by a configuration of option defaults for that target. Same as `ng build --configuration=XXX`.",
15+
"alias": "c"
16+
},
17+
"noBuild": {
18+
"type": "boolean",
19+
"default": false,
20+
"description": "Skip build process during deployment."
21+
},
22+
"outputPath": {
23+
"type": "string",
24+
"description": "This is one of the options you can freely choose according to your needs. --- We will 'deploy' to this folder."
25+
},
26+
"netlifyToken": {
27+
"type": "string",
28+
"description": "Acess token give you the ability to communicate with netlify over API."
29+
},
30+
"siteId": {
31+
"type": "string",
32+
"description": "Every netlify app have a API ID which uniquly identify that app."
33+
},
34+
"baseHref": {
35+
"type": "string",
36+
"description": "This is an example how to override the workspace set of options. --- Base url for the application being built. Same as `ng build --base-href=/XXX/`."
37+
},
38+
"create": {
39+
"type": "boolean",
40+
"default": false,
41+
"description": "Creates the site if it does not exists or no site id is set"
42+
},
43+
"withDeps": {
44+
"type": "boolean",
45+
"default": false,
46+
"description": "To be used with Nx if app needs to be built with-deps"
47+
}
4248
}
43-
}
4449
}

0 commit comments

Comments
 (0)