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

[core] core plugins update #82

Merged
merged 9 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
Expand Up @@ -2,3 +2,4 @@ dist
node_modules
packages/**/dist
packages/**/node_modules
packages/tests/**/fixtures
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ module.exports = {
env: {
jest: true,
},
globals: {
fail: 'readonly',
},
rules: {
'global-require': 'off',
'import/no-dynamic-require': 'off',
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
22 changes: 15 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,23 @@ stateDiagram-v2
factory: @dd/factory
core: @dd/core
types: Shared Types
context: Shared Global Context
helpers: Aggregated Helpers
atypes: Aggregated Types
aplugins: Aggregated List of Plugins
cli: Internal CLIs
internalPlugins: Internal Plugins
gitPlugin: Git Plugin
contextPlugin: Global Context Plugin

state internalPlugins {
gitPlugin
contextPlugin
}

state core {
getLogger()
types
context
internalPlugins
}

state published {
Expand Down Expand Up @@ -127,12 +134,13 @@ stateDiagram-v2
}

plugins --> factory: CONFIG_KEY\nhelpers\ntypes\ngetPlugins()
core --> tools
core --> factory
core --> plugins
core --> tests
core --> tools: types
core --> factory: Internal Plugins\ntypes
core --> plugins: getLogger()\ntypes
core --> tests: types
factory --> plugins: Global Context
factory --> published: Unplugin Factory
published --> [*]: types\nhelpers\ndatadogBundlerPlugin
published --> NPM: types\nhelpers\ndatadogBundlerPlugin
```

## Create a new plugin
Expand Down
11 changes: 11 additions & 0 deletions LICENSES-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ Component,Origin,Licence,Copyright
@jridgewell/source-map,npm,MIT,Justin Ridgewell (https://www.npmjs.com/package/@jridgewell/source-map)
@jridgewell/sourcemap-codec,npm,MIT,Rich Harris (https://www.npmjs.com/package/@jridgewell/sourcemap-codec)
@jridgewell/trace-mapping,npm,MIT,Justin Ridgewell (https://www.npmjs.com/package/@jridgewell/trace-mapping)
@jsonjoy.com/base64,virtual,Apache-2.0,streamich (https://github.com/jsonjoy-com/base64)
@jsonjoy.com/json-pack,virtual,Apache-2.0,streamich (https://github.com/jsonjoy-com/json-pack)
@jsonjoy.com/util,virtual,Apache-2.0,streamich (https://github.com/jsonjoy-com/util)
@kwsites/file-exists,npm,MIT,Steve King (https://www.npmjs.com/package/@kwsites/file-exists)
@kwsites/promise-deferred,npm,MIT,Steve King (https://www.npmjs.com/package/@kwsites/promise-deferred)
@nodelib/fs.scandir,npm,MIT,(https://www.npmjs.com/package/@nodelib/fs.scandir)
@nodelib/fs.stat,npm,MIT,(https://www.npmjs.com/package/@nodelib/fs.stat)
@nodelib/fs.walk,npm,MIT,(https://www.npmjs.com/package/@nodelib/fs.walk)
Expand Down Expand Up @@ -513,6 +518,7 @@ http-signature,npm,MIT,Joyent Inc (https://github.com/joyent/node-http-signatur
https-browserify,npm,MIT,James Halliday (https://github.com/substack/https-browserify)
human-signals,npm,Apache-2.0,ehmicky (https://git.io/JeluP)
husky,npm,MIT,Typicode (https://github.com/typicode/husky#readme)
hyperdyperid,npm,MIT,streamich (https://github.com/streamich/hyperdyperid)
ieee754,npm,BSD-3-Clause,Feross Aboukhadijeh (https://feross.org)
iferr,npm,MIT,Nadav Ivgi (https://github.com/shesek/iferr)
ignore,npm,MIT,kael (https://www.npmjs.com/package/ignore)
Expand Down Expand Up @@ -637,6 +643,7 @@ makeerror,npm,BSD-3-Clause,Naitik Shah (https://www.npmjs.com/package/makeerror)
map-cache,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/map-cache)
map-visit,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/map-visit)
md5.js,npm,MIT,Kirill Fomichev (https://github.com/crypto-browserify/md5.js)
memfs,npm,Apache-2.0,streamich (https://github.com/streamich/memfs)
memory-fs,npm,MIT,Tobias Koppers @sokra (https://github.com/webpack/memory-fs)
merge-stream,npm,MIT,Stephen Sugden (https://www.npmjs.com/package/merge-stream)
merge2,npm,MIT,(https://github.com/teambition/merge2)
Expand Down Expand Up @@ -793,12 +800,14 @@ shebang-command,npm,MIT,Kevin Mårtensson (github.com/kevva)
shebang-regex,npm,MIT,Sindre Sorhus (sindresorhus.com)
side-channel,npm,MIT,Jordan Harband (https://github.com/ljharb/side-channel#readme)
signal-exit,npm,ISC,Ben Coe (https://github.com/tapjs/signal-exit)
simple-git,npm,MIT,Steve King (https://www.npmjs.com/package/simple-git)
sisteransi,npm,MIT,Terkel Gjervig (https://terkel.com)
slash,npm,MIT,Sindre Sorhus (sindresorhus.com)
slice-ansi,npm,MIT,(https://www.npmjs.com/package/slice-ansi)
snapdragon,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/snapdragon)
snapdragon-node,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/snapdragon-node)
snapdragon-util,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/snapdragon-util)
sonic-forest,virtual,Apache-2.0,streamich (https://github.com/streamich/sonic-forest)
source-list-map,npm,MIT,Tobias Koppers @sokra (https://github.com/webpack/source-list-map)
source-map,npm,BSD-3-Clause,Nick Fitzgerald (https://github.com/mozilla/source-map)
source-map-resolve,npm,MIT,Simon Lydell (https://www.npmjs.com/package/source-map-resolve)
Expand Down Expand Up @@ -836,6 +845,7 @@ terser,npm,BSD-2-Clause,Mihai Bazon (https://terser.org)
terser-webpack-plugin,virtual,MIT,webpack Contrib Team (https://github.com/webpack-contrib/terser-webpack-plugin)
test-exclude,npm,ISC,Ben Coe (https://istanbul.js.org/)
text-table,npm,MIT,James Halliday (https://github.com/substack/text-table)
thingies,virtual,Unlicense,streamich (https://github.com/streamich/thingies)
through,npm,MIT,Dominic Tarr (https://github.com/dominictarr/through)
through2,npm,MIT,Rod Vagg (https://github.com/rvagg)
timers-browserify,npm,MIT,J. Ryan Stinnett (https://github.com/jryans/timers-browserify)
Expand All @@ -846,6 +856,7 @@ to-object-path,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/to-objec
to-regex,npm,MIT,Jon Schlinkert (https://github.com/jonschlinkert/to-regex)
to-regex-range,npm,MIT,Jon Schlinkert (https://github.com/micromatch/to-regex-range)
tough-cookie,npm,BSD-3-Clause,Jeremy Stashewsky (https://github.com/salesforce/tough-cookie)
tree-dump,virtual,UNKNOWN,streamich (https://github.com/streamich/tree-dump)
ts-api-utils,virtual,MIT,JoshuaKGoldberg (https://www.npmjs.com/package/ts-api-utils)
ts-jest,virtual,MIT,Kulshekhar Kabra (https://kulshekhar.github.io/ts-jest)
ts-node,virtual,MIT,Blake Embrey (https://typestrong.org/ts-node)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
}
},
"devDependencies": {
"@types/node": "18.15.3",
"@types/node": "^18",
"@typescript-eslint/eslint-plugin": "7.5.0",
"@typescript-eslint/parser": "7.5.0",
"eslint": "8.57.0",
Expand Down
41 changes: 40 additions & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
# Datadog Build Plugins Core

A set of core helpers to use within the Datadog Build Plugins.
A set of core functionalities to use within the Datadog Build Plugins.

## Plugins

### Global Context

Offers to share a global context between all the plugins.

```typescript
type GlobalContext = {
cwd: string;
version: string;
bundler: {
name: string;
config?: any;
};
};
```

### Git Plugins

Adds repository data to the global context.

```typescript
{
// Added to the global context.
git?: {
hash: string;
remote: string;
trackedFilesMatcher: {
matchSourcemap: (path: string, onSourceFound: (): void): string[];
matchSources: (sources: string[]): string[];
rawTrackedFilesList: (): string[];
};
}
}
```

> [!NOTE]
> This won't be added if `options.disabledGit = true` or `options.rum.sourcemaps.disabledGit = true`.
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
},
"dependencies": {
"chalk": "2.3.1",
"simple-git": "3.25.0",
"unplugin": "1.10.1"
},
"devDependencies": {
"@types/chalk": "2.2.0",
"@types/node": "18.15.3",
"@types/node": "^18",
"typescript": "5.4.3"
}
}
4 changes: 2 additions & 2 deletions packages/core/src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import c from 'chalk';

import type { LogLevel } from './types';

export type Logger = (text: any, type?: LogLevel) => void;

const log = (text: any, level: LogLevel, type: LogLevel = 'debug', name?: string) => {
let color = c;
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -33,8 +35,6 @@ const log = (text: any, level: LogLevel, type: LogLevel = 'debug', name?: string
}
};

export type Logger = (text: any, type?: LogLevel) => void;

export const getLogger =
(level: LogLevel = 'warn', name?: string): Logger =>
(text: any, type: LogLevel = 'debug') =>
Expand Down
60 changes: 0 additions & 60 deletions packages/core/src/plugins/cross-helpers/index.ts

This file was deleted.

126 changes: 126 additions & 0 deletions packages/core/src/plugins/git/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.

import type { SimpleGit, BranchSummary } from 'simple-git';
import { simpleGit } from 'simple-git';
import { URL } from 'url';

import type { RepositoryData } from '../../types';

import { TrackedFilesMatcher } from './trackedFilesMatcher';

// Returns a configured SimpleGit.
export const newSimpleGit = async (cwd?: string): Promise<SimpleGit> => {
const options = {
baseDir: cwd || process.cwd(),
binary: 'git',
// We are invoking at most 3 git commands at the same time.
maxConcurrentProcesses: 3,
};
try {
// Attempt to set the baseDir to the root of the repository so the 'git ls-files' command
// returns the tracked files paths relative to the root of the repository.
const git = simpleGit(options);
const root = await git.revparse('--show-toplevel');
options.baseDir = root;
} catch {
// Ignore exception as it will fail if we are not inside a git repository.
}

return simpleGit(options);
};

// Returns the remote of the current repository.
export const gitRemote = async (git: SimpleGit): Promise<string> => {
const remotes = await git.getRemotes(true);
if (remotes.length === 0) {
throw new Error('No git remotes available');
}
const defaultRemote = await getDefaultRemoteName(git);

for (const remote of remotes) {
if (remote.name === defaultRemote) {
return stripCredentials(remote.refs.push);
}
}

// Falling back to picking the first remote in the list if the default remote is not found.
return stripCredentials(remotes[0].refs.push);
};

export const getDefaultRemoteName = async (git: SimpleGit): Promise<string> => {
try {
return (await git.getConfig('clone.defaultRemoteName'))?.value ?? 'origin';
} catch (e) {
return 'origin';
}
};

// StripCredentials removes credentials from a remote HTTP url.
export const stripCredentials = (remote: string) => {
try {
const url = new URL(remote);
url.username = '';
url.password = '';

return url.toString();
} catch {
return remote;
}
};

// Returns the hash of the current repository.
export const gitHash = async (git: SimpleGit): Promise<string> => git.revparse('HEAD');

// Returns the tracked files of the current repository.
export const gitTrackedFiles = async (git: SimpleGit): Promise<string[]> => {
const files = await git.raw('ls-files');

return files.split(/\r\n|\r|\n/);
};

export const gitBranch = async (git: SimpleGit): Promise<BranchSummary> => git.branch();

export const gitMessage = async (git: SimpleGit): Promise<string> =>
git.show(['-s', '--format=%s']);

export const gitAuthorAndCommitter = async (git: SimpleGit): Promise<string> =>
git.show(['-s', '--format=%an,%ae,%aI,%cn,%ce,%cI']);

export const gitRepositoryURL = async (git: SimpleGit): Promise<string> =>
git.listRemote(['--get-url']);

// Returns the current hash and remote as well as a TrackedFilesMatcher.
//
// To obtain the list of tracked files paths tied to a specific sourcemap, invoke the 'matchSourcemap' method.
export const getRepositoryData = async (
git: SimpleGit,
repositoryURL?: string | undefined,
): Promise<RepositoryData> => {
// Invoke git commands to retrieve the remote, hash and tracked files.
// We're using Promise.all instead of Promise.allSettled since we want to fail early if
// any of the promises fails.
let remote: string;
let hash: string;
let trackedFiles: string[];

if (repositoryURL) {
[hash, trackedFiles] = await Promise.all([gitHash(git), gitTrackedFiles(git)]);
remote = repositoryURL;
} else {
[remote, hash, trackedFiles] = await Promise.all([
gitRemote(git),
gitHash(git),
gitTrackedFiles(git),
]);
}

const data = {
hash,
remote,
trackedFilesMatcher: new TrackedFilesMatcher(trackedFiles),
};

return data;
};
Loading
Loading