Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Type Declarations & Update Deps #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib/
132 changes: 132 additions & 0 deletions lib/config/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { GitLabCi } from "../types";
declare type MacroArgs = Record<string, any>;
/**
* A global OOP-style GitLab CI configurator.
*/
declare class Config {
/**
* Holding the complete GitLab CI configuration as plain object instead
* of classes so all is done within this class.
*/
private plain;
/**
* See macro() method.
*/
private macros;
/**
* See patch() method.
*/
private patchers;
/**
* REST API handler.
*/
private gapi?;
/**
* Get the REST API handler.
*
* @see https://www.npmjs.com/package/node-gitlab
*/
get api(): import("@gitbeaker/core/dist/types").Gitlab<boolean>;
/**
* The top-level `workflow:` key applies to the entirety of a pipeline, and will determine whether
* or not a pipeline is created. It currently accepts a single `rules:` key that operates similarly
* to `rules:` defined within jobs, enabling dynamic configuration of the pipeline.
*/
workflow(workflow: GitLabCi["workflow"]): void;
/**
* `stages` is used to define stages that can be used by jobs and is defined globally.
*
* @see https://devowl.io/knowledge-base/success-message-but-when-reloading-the-page-i-do-not-see-a-new-item/
*/
stages(...stages: string[]): void;
/**
* Some parameters can be set globally as the default for all jobs using the `default:` keyword.
* Default parameters can then be overridden by job-specific configuration.
*
* @see https://docs.gitlab.com/ee/ci/yaml/#global-defaults
*/
defaults(defaults: GitLabCi["default"]): void;
/**
* GitLab CI/CD allows you to define variables inside .gitlab-ci.yml that are then passed in the job environment.
* They can be set globally and per-job. When the variables keyword is used on a job level, it will override the global YAML
* variables and predefined ones of the same name.
*
* @see https://docs.gitlab.com/ee/ci/yaml/#variables
*/
variable(key: string, value: GitLabCi["variables"][0]): void;
/**
* Get variable from job.
*/
getVariable(job: string, key: string): string | number | import("../types").VariableObject;
/**
* Register a macro. A macro can be used to define jobs from a given variable map.
*
* @param key
* @param callback
*/
macro<T extends MacroArgs>(key: string, callback: (config: Config, args: T) => void): void;
/**
* Apply a macro.
*
* @param key
* @param args
*/
from<T extends MacroArgs>(key: string, args: T): void;
/**
* Allows to run a callback on the resulting GitLab CI Yaml object (after recursively applying
* extends and macros).
*/
patch(callback: Config["patchers"][0]): void;
/**
* A job is defined as a list of parameters that define the job’s behavior.
*
* @see https://docs.gitlab.com/ee/ci/yaml/#configuration-parameters
* @param name The new job name
* @param job Job definition
* @param hidden See https://docs.gitlab.com/ee/ci/yaml/#hide-jobs for more infos
*/
job(name: string, job: GitLabCi["jobs"][0], hidden?: boolean): void;
/**
* Similar to [`extends`](https://docs.gitlab.com/ee/ci/yaml/#extends) but it uses
* a deep-merge mechanism instead of the built-in extend functionality of GitLab CI.
* This ensures more granular configuration!
*
* @param fromName The job name you want to extend from
* @param name The new job name
* @param job Job definition
* @param hidden See https://docs.gitlab.com/ee/ci/yaml/#hide-jobs for more infos
*/
extends(fromName: string | string[], name: string, job: GitLabCi["jobs"][0], hidden?: boolean): void;
/**
* Include further `.ts` configurations by a glob. This is similar to [`include:local`](https://docs.gitlab.com/ee/ci/yaml/#includelocal)
* but this implementation should be used instead!
*
* @param cwd Current working directy, use `process.cwd()`
* @param globs See https://www.npmjs.com/package/glob for more information
*/
include(cwd: string, globs: string[]): Promise<void>;
/**
* Get the whole configuration as yaml-serializable object.
*/
getPlainObject(): GitLabCi;
/**
* Check if files got changed by a commit by a regexp. E. g. `^\.vscode\/launch\.json$`.
*/
hasChanged(regexp?: RegExp, sha?: string, cwd?: string): Promise<boolean | string[]>;
private recursivelyExtend;
/**
* Resolves all `extends` and puts it in correct order.
*
* @param pipeline
*/
private resolveExtends;
/**
* Clear temporary hold variables.
*
* @param pipeline
*/
private clear;
}
declare type CreateConfigFunction = () => Promise<Config>;
declare type ExtendConfigFunction = (config: Config) => void;
export { Config, CreateConfigFunction, ExtendConfigFunction, MacroArgs };
82 changes: 44 additions & 38 deletions lib/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ var __assign = (this && this.__assign) || function () {
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
Expand Down Expand Up @@ -93,6 +97,22 @@ var Config = /** @class */ (function () {
*/
this.patchers = [];
}
Object.defineProperty(Config.prototype, "api", {
/**
* Get the REST API handler.
*
* @see https://www.npmjs.com/package/node-gitlab
*/
get: function () {
if (!this.gapi) {
var _a = process.env, CI_JOB_TOKEN = _a.CI_JOB_TOKEN, GITLAB_TOKEN = _a.GITLAB_TOKEN, CI_SERVER_URL = _a.CI_SERVER_URL;
this.gapi = new node_1.Gitlab(__assign(__assign({ host: CI_SERVER_URL, token: GITLAB_TOKEN }, (GITLAB_TOKEN ? {} : { jobToken: CI_JOB_TOKEN })), { rejectUnauthorized: true }));
}
return this.gapi;
},
enumerable: false,
configurable: true
});
/**
* The top-level `workflow:` key applies to the entirety of a pipeline, and will determine whether
* or not a pipeline is created. It currently accepts a single `rules:` key that operates similarly
Expand All @@ -102,7 +122,7 @@ var Config = /** @class */ (function () {
if (!this.plain.workflow) {
this.plain.workflow = { rules: [] };
}
this.plain.workflow = deepmerge_1.default(this.plain.workflow, workflow);
this.plain.workflow = (0, deepmerge_1.default)(this.plain.workflow, workflow);
};
/**
* `stages` is used to define stages that can be used by jobs and is defined globally.
Expand Down Expand Up @@ -130,7 +150,7 @@ var Config = /** @class */ (function () {
if (!this.plain.default) {
this.plain.default = {};
}
this.plain.default = deepmerge_1.default(this.plain.default, defaults);
this.plain.default = (0, deepmerge_1.default)(this.plain.default, defaults);
};
/**
* GitLab CI/CD allows you to define variables inside .gitlab-ci.yml that are then passed in the job environment.
Expand Down Expand Up @@ -164,7 +184,7 @@ var Config = /** @class */ (function () {
*/
Config.prototype.macro = function (key, callback) {
if (this.macros[key]) {
throw new Error("Macro " + key + " already defined! You are not allowed to overwrite it.");
throw new Error("Macro ".concat(key, " already defined! You are not allowed to overwrite it."));
}
this.macros[key] = callback;
};
Expand All @@ -176,7 +196,7 @@ var Config = /** @class */ (function () {
*/
Config.prototype.from = function (key, args) {
if (!this.macros[key]) {
throw new Error("Macro " + key + " not found, please register it with Config#macro! Consider also, that you need to register the macro before you execute from it.");
throw new Error("Macro ".concat(key, " not found, please register it with Config#macro! Consider also, that you need to register the macro before you execute from it."));
}
this.macros[key](this, args);
};
Expand All @@ -200,13 +220,13 @@ var Config = /** @class */ (function () {
if (!this.plain.jobs) {
this.plain.jobs = {};
}
var useName = hidden && !name.startsWith(".") ? "." + name : name;
var useName = hidden && !name.startsWith(".") ? ".".concat(name) : name;
if (!this.plain.jobs[useName]) {
this.plain.jobs[useName] = job;
console.log("Job \"" + useName + "\" created successfully!");
console.log("Job \"".concat(useName, "\" created successfully!"));
}
else {
console.info("Job \"" + useName + "\" already exists, skipping...");
console.info("Job \"".concat(useName, "\" already exists, skipping..."));
}
};
/**
Expand All @@ -221,7 +241,7 @@ var Config = /** @class */ (function () {
*/
Config.prototype.extends = function (fromName, name, job, hidden) {
if (hidden === void 0) { hidden = false; }
this.job(name, deepmerge_1.default(job, { extends: Array.isArray(fromName) ? fromName : [fromName] }), hidden);
this.job(name, (0, deepmerge_1.default)(job, { extends: Array.isArray(fromName) ? fromName : [fromName] }), hidden);
};
/**
* Include further `.ts` configurations by a glob. This is similar to [`include:local`](https://docs.gitlab.com/ee/ci/yaml/#includelocal)
Expand All @@ -241,7 +261,7 @@ var Config = /** @class */ (function () {
case 1:
if (!(_i < globs_1.length)) return [3 /*break*/, 7];
glob = globs_1[_i];
files = glob_1.sync(glob, {
files = (0, glob_1.sync)(glob, {
absolute: true,
cwd: cwd,
dot: true,
Expand All @@ -251,7 +271,7 @@ var Config = /** @class */ (function () {
case 2:
if (!(_a < files_1.length)) return [3 /*break*/, 6];
file = files_1[_a];
console.log("Include file \"" + file + "...\"");
console.log("Include file \"".concat(file, "...\""));
return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require(file)); })];
case 3:
exported = _b.sent();
Expand Down Expand Up @@ -292,22 +312,6 @@ var Config = /** @class */ (function () {
delete copy.jobs;
return copy;
};
Object.defineProperty(Config.prototype, "api", {
/**
* Get the REST API handler.
*
* @see https://www.npmjs.com/package/node-gitlab
*/
get: function () {
if (!this.gapi) {
var _a = process.env, CI_JOB_TOKEN = _a.CI_JOB_TOKEN, GITLAB_TOKEN = _a.GITLAB_TOKEN, CI_SERVER_URL = _a.CI_SERVER_URL;
this.gapi = new node_1.Gitlab(__assign(__assign({ host: CI_SERVER_URL, token: GITLAB_TOKEN }, (GITLAB_TOKEN ? {} : { jobToken: CI_JOB_TOKEN })), { rejectUnauthorized: true }));
}
return this.gapi;
},
enumerable: false,
configurable: true
});
/**
* Check if files got changed by a commit by a regexp. E. g. `^\.vscode\/launch\.json$`.
*/
Expand All @@ -318,11 +322,11 @@ var Config = /** @class */ (function () {
return __generator(this, function (_a) {
useSha = sha ||
process.env.CI_COMMIT_SHA ||
child_process_1.execSync("git rev-parse HEAD", {
(0, child_process_1.execSync)("git rev-parse HEAD", {
encoding: "utf-8",
cwd: cwd,
});
list = child_process_1.execSync("git diff-tree --no-commit-id --name-only -r " + useSha, {
list = (0, child_process_1.execSync)("git diff-tree --no-commit-id --name-only -r " + useSha, {
encoding: "utf-8",
cwd: cwd,
}).toString();
Expand All @@ -346,11 +350,11 @@ var Config = /** @class */ (function () {
if ((_a = pipeline.jobs) === null || _a === void 0 ? void 0 : _a[from]) {
jobKey = from;
}
else if ((_b = pipeline.jobs) === null || _b === void 0 ? void 0 : _b["." + from]) {
jobKey = "." + from;
else if ((_b = pipeline.jobs) === null || _b === void 0 ? void 0 : _b[".".concat(from)]) {
jobKey = ".".concat(from);
}
if (!jobKey) {
console.warn("The job \"" + from + "\" does not exist, skipping...");
console.warn("The job \"".concat(from, "\" does not exist, skipping..."));
continue;
}
var jobObj = pipeline.jobs[jobKey];
Expand All @@ -365,20 +369,21 @@ var Config = /** @class */ (function () {
* @param pipeline
*/
Config.prototype.resolveExtends = function (pipeline) {
var jobIds = Object.keys(pipeline.jobs);
var _a;
var jobIds = Object.keys((_a = pipeline.jobs) !== null && _a !== void 0 ? _a : {});
for (var _i = 0, jobIds_1 = jobIds; _i < jobIds_1.length; _i++) {
var key = jobIds_1[_i];
var job = pipeline.jobs[key];
if (job.extends && !key.startsWith(".")) {
this.recursivelyExtend(pipeline, job);
var result = {};
var needsExtends = job.needsExtends;
for (var _a = 0, needsExtends_1 = needsExtends; _a < needsExtends_1.length; _a++) {
var extendKey = needsExtends_1[_a];
result = deepmerge_1.default(result, pipeline.jobs[extendKey]);
for (var _b = 0, needsExtends_1 = needsExtends; _b < needsExtends_1.length; _b++) {
var extendKey = needsExtends_1[_b];
result = (0, deepmerge_1.default)(result, pipeline.jobs[extendKey]);
}
// The main job definition has highest priority
pipeline.jobs[key] = deepmerge_1.default(result, job);
pipeline.jobs[key] = (0, deepmerge_1.default)(result, job);
}
}
};
Expand All @@ -388,8 +393,9 @@ var Config = /** @class */ (function () {
* @param pipeline
*/
Config.prototype.clear = function (pipeline) {
var _a;
// Finally, remove all existing `extends`
var jobIds = Object.keys(pipeline.jobs);
var jobIds = Object.keys((_a = pipeline.jobs) !== null && _a !== void 0 ? _a : {});
for (var _i = 0, jobIds_2 = jobIds; _i < jobIds_2.length; _i++) {
var key = jobIds_2[_i];
var job = pipeline.jobs[key];
Expand Down
Loading