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

chore: TS check #122

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = {
"no-param-reassign": [0],
"no-continue": [0],
"no-unused-vars": [
2,
1,
{
vars: "all",
args: "after-used",
Expand Down
34 changes: 25 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,36 @@
"packages/*"
],
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/lodash.camelcase": "^4.3.7",
"@types/lodash.clonedeep": "^4.5.7",
"@types/lodash.defaults": "^4.2.7",
"@types/lodash.get": "^4.4.7",
"@types/lodash.omit": "^4.5.7",
"@types/lodash.pick": "^4.4.7",
"@types/lodash.set": "^4.3.7",
"@types/micromatch": "^4.0.2",
"@types/node": "^20.10.5",
"@types/pino": "^7.0.5",
"commit-and-tag-version": "^11.0.0",
"docsify-cli": "^4.4.4",
"eslint": "^8.26.0",
"eslint": "^8.31.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-prettier": "^8.6.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.0.0",
"eslint-plugin-jest": "^27.2.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"fs-extra": "^11.1.1",
"husky": "^8.0.0",
"eslint-plugin-prettier": "^4.2.1",
"fs-extra": "^11.1.0",
"husky": "^8.0.3",
"js-yaml": "^4.1.0",
"lint-staged": "^13.0.0",
"prettier": "^2.6.2",
"kubernetes-models": "^4.1.0",
"lint-staged": "^13.1.0",
"prettier": "^2.8.1",
"renovate": "^35.0.0",
"standard-version": "^9.5.0",
"typescript": "^4.9.4",
"~dev-tools": "workspace:^"
},
"resolutions": {
Expand Down Expand Up @@ -65,7 +79,9 @@
"docker:kontinuous:build": "DOCKER_BUILDKIT=1 docker build --progress=plain -f packages/kontinuous/Dockerfile -t kontinuous .",
"docker:kontinuous:run": "docker run -it -v $PWD:/workspace -v $(dirname $KUBECONFIG):/home/node/.kube -e KUBECONFIG=/home/node/.kube/$(basename $KUBECONFIG) kontinuous",
"docker:kontinuous": "yarn docker:kontinuous:build && yarn docker:kontinuous:run",
"logs:remote-webhook": "kubectl --context prod -n kontinuous-webhook logs -f --all-containers -l component=kontinuous-webhook"
"logs:remote-webhook": "kubectl --context prod -n kontinuous-webhook logs -f --all-containers -l component=kontinuous-webhook",
"upgrade:modjo": "yarn up -R \"@modjo-plugins/*\"",
"ts:check": "tsc -p tsconfig.json"
},
"lint-staged": {
"*.js": [
Expand Down
7 changes: 6 additions & 1 deletion packages/common/config/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ const configDependencyKey = require("~common/utils/config-dependency-key")
const lowerKeys = require("~common/utils/lower-keys")
const ctx = require("../ctx")
const patternMatch = require("../utils/pattern-match")
const loadStructuredConfig = require("../utils/load-structured-config")

const loadStructuredConfig =
/** @type {import("../utils/load-structured-config").LoadStructuredConfig} */ (
/** @type {unknown} */ (require("../utils/load-structured-config"))
)

const getGitRef = require("../utils/get-git-ref")
const getGitSha = require("../utils/get-git-sha")
const getGitUrl = require("../utils/get-git-url")
Expand Down
7 changes: 7 additions & 0 deletions packages/common/config/load-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const patternMatch = require("../utils/pattern-match")
const copyFilter = require("./copy-filter")
const loadGitOrgConfig = require("./load-git-org-config")

/**
*
* @param {*} config
* @param {import("../utils")["logger"]} logger
* @param {*} reloadConfig
* @returns {Promise<object>}
*/
module.exports = async (config, logger, reloadConfig) => {
logger.debug("🔻 load dependencies")

Expand Down
13 changes: 12 additions & 1 deletion packages/common/config/load-git-org-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@ const defaultsDeep = require("lodash.defaultsdeep")
const normalizeRepositoryUrl = require("~common/utils/normalize-repository-url")

const getGitRemoteDefaultBranch = require("~common/utils/get-git-remote-default-branch")
const parseGitUrl = require("~common/utils/parse-git-url")

const parseGitUrl =
/** @type {import("../utils/parse-git-url").ParseGithubUrl} */ (
/** @type {unknown} */ (require("~common/utils/parse-git-url"))
)

const downloadGitOrgConfig = require("./download-git-org-config")

/**
*
* @param {object} config
* @param {Function} reloadConfig
* @returns
*/
module.exports = async (config, reloadConfig) => {
const {
gitOrg,
Expand Down
20 changes: 19 additions & 1 deletion packages/common/utils/async-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ const getLogger = require("./get-logger")
const parseCommand = require("./parse-command")
const ProgramError = require("./program-error.class")

/**
*
* @param {import("child_process").ChildProcess} child
* @param {null | ((child: import("child_process").ChildProcessWithoutNullStreams) => void)} callback
* @param {Kontinuous.Patch.Context["logger"]} logger
* @param {{ignoreErrors?: string[]}} extraOptions
* @returns
*/
const promiseFromChildProcess = (child, callback, logger, extraOptions) => {
const { ignoreErrors = [] } = extraOptions
child.on("error", () => {}) // avoid crash on not found executable
/** @type {Buffer[]} */
const out = []
child.stdout.on("data", (data) => {
out.push(data)
})
/** @type {Buffer[]} */
const err = []
child.stderr.on("data", (data) => {
if (ignoreErrors.some((errCatch) => data.includes(errCatch))) {
Expand Down Expand Up @@ -39,7 +49,15 @@ const promiseFromChildProcess = (child, callback, logger, extraOptions) => {
})
})
}

/**
*
* @param {string|string[]} arg
* @param {Record<string, any>} options
* @param {((child: import("child_process").ChildProcess) => void)} [callback]
* @param {Kontinuous.Patch.Context["logger"]} logger
* @param {{ignoreErrors?: string[]}} extraOptions
* @returns
*/
module.exports = (
arg,
options = {},
Expand Down
4 changes: 2 additions & 2 deletions packages/common/utils/axios-retry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require("axios")
const axiosRetry = require("axios-retry")
const { default: axios } = require("axios")
const { default: axiosRetry } = require("axios-retry")

const kontinuousVersion = require("./kontinuous-version")

Expand Down
14 changes: 14 additions & 0 deletions packages/common/utils/check-namespace-is-available.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ const kubectlRetry = require("./kubectl-retry")

const getLogger = require("./get-logger")

/**
*
* @param {object} param0
* @param {string} param0.kubeconfig
* @param {string} param0.kubeconfigContext
* @param {string} param0.namespace
* @param {Kontinuous.Patch.Context["logger"]} [param0.logger]
* @param {Function} [param0.check]
* @param {boolean} [param0.bail]
* @param {boolean} [param0.surviveOnBrokenCluster]
* @param {import("./kubectl-retry").KubectlRetryOptions} [param0.retryOptions]
* @param {Function} [param0.kubectl]
* @returns
*/
module.exports = async ({
kubeconfig,
kubeconfigContext,
Expand Down
6 changes: 6 additions & 0 deletions packages/common/utils/deepmerge.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const mergeWith = require("lodash.mergewith")

/**
* Helm like deep merge
* @param {object} objValue
* @param {...object} srcValues
* @returns {object}
*/
module.exports = (objValue, ...srcValues) =>
mergeWith(objValue, ...srcValues, (oValue, srcValue) => {
if (Array.isArray(oValue)) {
Expand Down
11 changes: 11 additions & 0 deletions packages/common/utils/degit-tag-has-changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const asyncShell = require("./async-shell")
const normalizeRepositoryUrl = require("./normalize-repository-url")
const getLogger = require("./get-logger")

/** @type {Record<string, string>} */
const supported = {
github: ".com",
gitlab: ".com",
Expand All @@ -15,6 +16,11 @@ const supported = {
"git.sr.ht": ".ht",
}

/**
*
* @param {string} src
* @returns {{site:string, user:string, name:string, ref:string, url:string, ssh:string, subdir:string, mode:string}}
*/
function parse(src) {
const match =
/^(?:(?:https:\/\/)?([^:/]+\.[^:/]+)\/|git@([^:/]+)[:/]|([^/]+):)?([^/\s]+)\/([^/\s#]+)(?:((?:\/[^/\s#]+)+))?(?:\/)?(?:#(.+))?/.exec(
Expand Down Expand Up @@ -46,6 +52,11 @@ function parse(src) {
return { site: siteName, user, name, ref, url, ssh, subdir, mode }
}

/**
*
* @param {string} target
* @returns boolean
*/
module.exports = async (target, logger = getLogger()) => {
const base = path.join(os.homedir(), ".degit")
const repo = parse(target)
Expand Down
1 change: 1 addition & 0 deletions packages/common/utils/get-logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ctx = require("~common/ctx")
const createLogger = require("./direct-logger-factory")

/** @type {() => Kontinuous.Patch.Context["logger"]} */
module.exports = () => ctx.getDefault("logger") || createLogger()
4 changes: 4 additions & 0 deletions packages/common/utils/ignore-yarn-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const yarnNotIgnore = [
".yarn/cache",
]

/**
* @param {string} src
* @returns boolean
*/
module.exports = (src) => {
if (src.includes("node_modules/") || src.endsWith("node_modules")) {
return false
Expand Down
5 changes: 4 additions & 1 deletion packages/common/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module.exports = {
const utils = {
degit: require("tiged"),
fs: require("fs-extra"),
get logger() {
/** @type {Kontinuous.Patch.Context["logger"]} */
return require("./get-logger")()
},
asyncShell: require("./async-shell"),
Expand Down Expand Up @@ -119,3 +120,5 @@ module.exports = {
matchLinkRemap: require("./match-link-remap"),
detectKubeVersion: require("./detect-kube-version"),
}

module.exports = utils
4 changes: 4 additions & 0 deletions packages/common/utils/is-version-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
const versionTagSemverRe =
/^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-]+)*))?$/

/**
* @param {string} tag
* @returns boolean
*/
module.exports = (tag) => versionTagSemverRe.test(tag)
8 changes: 8 additions & 0 deletions packages/common/utils/kind-is-runnable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const kindsRunnable = ["Deployment", "Job", "DaemonSet", "StatefulSet"]

/**
*
* @param {Kontinuous.Manifest} manifest
* @returns {manifest is Kontinuous.RunnableManifest}
*/
module.exports = (manifest) => kindsRunnable.includes(manifest.kind)
38 changes: 37 additions & 1 deletion packages/common/utils/kubectl-retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ const getLogger = require("./get-logger")
const retriableOnBrokenCluster = require("./retriable-on-broken-cluster")
const retriableNetwork = require("./retriable-network")

/**
* @typedef {Object} KubectlRunOptions
* @prop {string} [kubeconfig]
* @prop {string} [kubeconfigContext]
* @prop {string[]} [ignoreErrors]
* @prop {string} [stdin]
* @prop {string} [abortSignal]
* @prop {import('child_process').ChildProcess[]} [collectProcesses]
*/

/**
*
* @param {string|string[]} kubectlArgs
* @param {KubectlRunOptions} options
* @returns void
*/
const kubectlRun = async (kubectlArgs, options = {}) => {
const {
kubeconfig,
Expand All @@ -29,7 +45,7 @@ const kubectlRun = async (kubectlArgs, options = {}) => {

return new Promise((resolve, reject) => {
const proc = spawn(cmd, args, {
encoding: "utf-8",
// encoding: "utf-8",
env: {
...process.env,
...(kubeconfig ? { KUBECONFIG: kubeconfig } : {}),
Expand Down Expand Up @@ -79,6 +95,26 @@ const kubectlRun = async (kubectlArgs, options = {}) => {
})
}

/**
* @typedef {Object} KubectlRetryOptions
* @prop {any} [logger]
* @prop {any} [sentry]
* @prop {string} [kubeconfig]
* @prop {string} [kubeconfigContext]
* @prop {Record<string, any>} [retryOptions]
* @prop {boolean} [logError]
* @prop {boolean} [logInfo]
* @prop {string[]} [ignoreErrors]
* @prop {import('child_process').ChildProcess[]} [collectProcesses]
* @prop {boolean} [surviveOnBrokenCluster]
*/

/**
*
* @param {string|string[]} kubectlArgs
* @param {KubectlRetryOptions} options
* @returns string
*/
module.exports = async (kubectlArgs, options = {}) => {
const {
logger = getLogger(),
Expand Down
21 changes: 21 additions & 0 deletions packages/common/utils/load-structured-config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export type CompilerConfig = object;

export type StructuredconfigOptions = {
configBasename?: string;
inlineConfigs?: object[];
configDirs?: string[];
configPreCompilers?: CompilerConfig[];
configOverride?: object;
env: Record<string, string>;
options: object;
mergeWith?: void;
emptyAsUndefined?: boolean;
rootConfig: object;
configMeta: object;
};

export type LoadStructuredConfig = (
arg0: StructuredconfigOptions
) => Promise<Record<string, any>>;

export default LoadStructuredConfig;
3 changes: 2 additions & 1 deletion packages/common/utils/load-structured-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const undefinedCheck = (val) => val === undefined
const emptyAsUndefinedCheck = (val) =>
val === undefined || val === "" || val === null

module.exports = async ({
/** @type {import("./load-structured-config") } */
module.exports = /** @type {import("./load-structured-config") } */ async ({
configBasename = "config",
inlineConfigs = [],
configDirs = [],
Expand Down
2 changes: 1 addition & 1 deletion packages/common/utils/need-helm.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const download = async (options) => {
switch (platform) {
case "darwin":
break
case "windows":
case "win32":
ext = ".exe"
break
case "linux":
Expand Down
2 changes: 1 addition & 1 deletion packages/common/utils/need-kapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const download = async (options) => {
switch (platform) {
case "darwin":
break
case "windows":
case "win32":
ext = ".exe"
break
case "linux":
Expand Down
2 changes: 1 addition & 1 deletion packages/common/utils/need-kubectl.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const download = async (options) => {
switch (platform) {
case "darwin":
break
case "windows":
case "win32":
ext = ".exe"
break
case "linux":
Expand Down
Loading
Loading