or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

additional-utilities.mdasset-management.mdexecutors.mdgenerators.mdindex.mdpackage-management.mdtypescript-utilities.md

additional-utilities.mddocs/

0

# Additional Utilities

1

2

@nx/js provides several additional utility functions for package management, lock files, dependency handling, and development workflows.

3

4

## Capabilities

5

6

### Package JSON Management

7

8

Advanced package.json copying and management with watch mode support and dependency handling.

9

10

```typescript { .api }

11

/**

12

* Copies and updates package.json files with dependency management and watch support

13

* @param options - Package JSON copy configuration

14

* @param context - Nx executor context

15

* @returns Promise resolving to success status with optional stop function for watch mode

16

*/

17

function copyPackageJson(

18

options: CopyPackageJsonOptions,

19

context: ExecutorContext

20

): Promise<CopyPackageJsonResult>;

21

22

interface CopyPackageJsonOptions extends Omit<UpdatePackageJsonOption, 'projectRoot'> {

23

watch?: boolean; // Enable watch mode for automatic updates

24

extraDependencies?: DependentBuildableProjectNode[]; // Additional dependencies to include

25

overrideDependencies?: DependentBuildableProjectNode[]; // Override dependency list

26

}

27

28

interface CopyPackageJsonResult {

29

success?: boolean; // Operation success status

30

stop?: () => void; // Stop function for watch mode (only when watch: true)

31

}

32

```

33

34

**Usage Example:**

35

36

```typescript

37

import { ExecutorContext } from "@nx/devkit";

38

import { copyPackageJson } from "@nx/js";

39

40

// Copy package.json with watch mode

41

const result = await copyPackageJson({

42

outputPath: 'dist/libs/my-lib',

43

generatePackageJson: true,

44

watch: true

45

}, context);

46

47

// Stop watching when done

48

if (result.stop) {

49

result.stop();

50

}

51

```

52

53

### Entry Points Management

54

55

Creates and manages package.json entry points for multi-format publishing.

56

57

```typescript { .api }

58

/**

59

* Creates entry points configuration for package.json exports field

60

* @param main - Main entry file path

61

* @param outputPath - Output directory path

62

* @param projectRoot - Project root directory

63

* @param additionalEntryPoints - Additional entry points to include

64

* @param format - Output format configuration

65

* @returns Entry points configuration object

66

*/

67

function createEntryPoints(

68

main: string,

69

outputPath: string,

70

projectRoot: string,

71

additionalEntryPoints?: string[],

72

format?: SupportedFormat[]

73

): Record<string, any>;

74

```

75

76

**Usage Example:**

77

78

```typescript

79

import { createEntryPoints } from "@nx/js";

80

81

// Create entry points for dual CJS/ESM publishing

82

const entryPoints = createEntryPoints(

83

'src/index.ts',

84

'dist/libs/my-lib',

85

'libs/my-lib',

86

['src/feature.ts'],

87

['cjs', 'esm']

88

);

89

```

90

91

### Lock File Management

92

93

Utilities for creating and managing package manager lock files.

94

95

```typescript { .api }

96

/**

97

* Creates a lock file for the specified package manager

98

* @param packageManager - Package manager type

99

* @param workspaceRoot - Workspace root directory

100

* @param projectRoot - Project root directory

101

* @param packageJson - Package.json contents

102

* @returns Promise resolving to lock file creation result

103

*/

104

function createLockFile(

105

packageManager: 'npm' | 'yarn' | 'pnpm',

106

workspaceRoot: string,

107

projectRoot: string,

108

packageJson: any

109

): Promise<void>;

110

111

/**

112

* Gets the appropriate lock file name for the package manager

113

* @param packageManager - Package manager type

114

* @returns Lock file name

115

*/

116

function getLockFileName(packageManager: 'npm' | 'yarn' | 'pnpm'): string;

117

```

118

119

**Usage Example:**

120

121

```typescript

122

import { createLockFile, getLockFileName } from "@nx/js";

123

124

// Create lock file for npm

125

await createLockFile('npm', '/workspace', 'libs/my-lib', packageJson);

126

127

// Get lock file name

128

const lockFileName = getLockFileName('pnpm'); // Returns 'pnpm-lock.yaml'

129

```

130

131

### Local Registry Scripts

132

133

Utilities for managing local package registry scripts and configurations.

134

135

```typescript { .api }

136

/**

137

* Adds local registry scripts to package.json for development workflows

138

* @param tree - Virtual file system tree

139

* @param options - Local registry configuration options

140

* @returns Updated package.json content

141

*/

142

function addLocalRegistryScripts(

143

tree: Tree,

144

options: LocalRegistryOptions

145

): void;

146

147

interface LocalRegistryOptions {

148

port?: number; // Registry port (default: 4873)

149

registryUrl?: string; // Custom registry URL

150

scopes?: string[]; // Package scopes to configure

151

}

152

```

153

154

**Usage Example:**

155

156

```typescript

157

import { Tree } from "@nx/devkit";

158

import { addLocalRegistryScripts } from "@nx/js";

159

160

// Add local registry scripts

161

addLocalRegistryScripts(tree, {

162

port: 4873,

163

scopes: ['@myorg']

164

});

165

```

166

167

### TypeScript Dependencies

168

169

Manages tslib and other TypeScript runtime dependencies.

170

171

```typescript { .api }

172

/**

173

* Adds tslib dependencies to the project based on TypeScript configuration

174

* @param tree - Virtual file system tree

175

* @param options - Dependency addition options

176

* @returns Generator callback for package installation

177

*/

178

function addTslibDependencies(

179

tree: Tree,

180

options: TslibDependencyOptions

181

): GeneratorCallback;

182

183

interface TslibDependencyOptions {

184

projectRoot: string; // Project root directory

185

skipPackageJson?: boolean; // Skip package.json updates

186

importHelpers?: boolean; // Enable TypeScript importHelpers

187

}

188

```

189

190

**Usage Example:**

191

192

```typescript

193

import { Tree } from "@nx/devkit";

194

import { addTslibDependencies } from "@nx/js";

195

196

// Add tslib dependencies

197

const installTask = addTslibDependencies(tree, {

198

projectRoot: 'libs/my-lib',

199

importHelpers: true

200

});

201

202

// Execute installation

203

await installTask();

204

```

205

206

### Variable Validation

207

208

Utility for validating JavaScript/TypeScript variable names.

209

210

```typescript { .api }

211

/**

212

* Validates if a string is a valid JavaScript/TypeScript variable name

213

* @param name - Variable name to validate

214

* @returns True if valid variable name, false otherwise

215

*/

216

function isValidVariable(name: string): boolean;

217

```

218

219

**Usage Example:**

220

221

```typescript

222

import { isValidVariable } from "@nx/js";

223

224

// Validate variable names

225

console.log(isValidVariable('myVariable')); // true

226

console.log(isValidVariable('my-variable')); // false

227

console.log(isValidVariable('123invalid')); // false

228

```