|
| 1 | +const Arborist = require('@npmcli/arborist') |
| 2 | +const getWorkspaces = require('../workspaces/get-workspaces.js') |
| 3 | +const { join, relative, dirname } = require('path') |
| 4 | +const packlist = require('npm-packlist') |
| 5 | +const fs = require('@npmcli/fs') |
| 6 | + |
| 7 | +const BaseCommand = require('../base-command.js') |
| 8 | + |
| 9 | +class Copy extends BaseCommand { |
| 10 | + static description = 'Copy package to new location' |
| 11 | + |
| 12 | + static name = 'copy' |
| 13 | + |
| 14 | + static params = [ |
| 15 | + 'omit', |
| 16 | + 'workspace', |
| 17 | + 'workspaces', |
| 18 | + 'include-workspace-root', |
| 19 | + ] |
| 20 | + |
| 21 | + static ignoreImplicitWorkspace = false |
| 22 | + |
| 23 | + static usage = ['<destination>'] |
| 24 | + |
| 25 | + async exec (args) { |
| 26 | + await this.copyTo(args, true, new Set([])) |
| 27 | + } |
| 28 | + |
| 29 | + // called when --workspace or --workspaces is passed. |
| 30 | + async execWorkspaces (args, filters) { |
| 31 | + const workspaces = await getWorkspaces(filters, { |
| 32 | + path: this.npm.localPrefix, |
| 33 | + }) |
| 34 | + |
| 35 | + await this.copyTo( |
| 36 | + args, |
| 37 | + this.includeWorkspaceRoot, |
| 38 | + new Set(workspaces.values())) |
| 39 | + } |
| 40 | + |
| 41 | + async copyTo (args, includeWorkspaceRoot, workspaces) { |
| 42 | + if (args.length !== 1) { |
| 43 | + throw this.usageError('Missing required destination argument') |
| 44 | + } |
| 45 | + const opts = { |
| 46 | + ...this.npm.flatOptions, |
| 47 | + path: this.npm.localPrefix, |
| 48 | + log: this.npm.log, |
| 49 | + } |
| 50 | + const destination = args[0] |
| 51 | + const omit = new Set(this.npm.flatOptions.omit) |
| 52 | + |
| 53 | + const tree = await new Arborist(opts).loadActual() |
| 54 | + |
| 55 | + // map of node to location in destination. |
| 56 | + const destinations = new Map() |
| 57 | + |
| 58 | + // calculate the root set of packages. |
| 59 | + if (includeWorkspaceRoot) { |
| 60 | + const to = join(destination, tree.location) |
| 61 | + destinations.set(tree, to) |
| 62 | + } |
| 63 | + for (const edge of tree.edgesOut.values()) { |
| 64 | + if (edge.workspace && workspaces.has(edge.to.realpath)) { |
| 65 | + const to = join(destination, edge.to.location) |
| 66 | + destinations.set(edge.to, to) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // copy the root set of packages and their dependencies. |
| 71 | + for (const [node, dest] of destinations) { |
| 72 | + if (node.isLink && node.target) { |
| 73 | + const targetPath = destinations.get(node.target) |
| 74 | + if (targetPath == null) { |
| 75 | + // This is the first time the link target was seen, it will be the |
| 76 | + // only copy in dest, other links to the same target will link to |
| 77 | + // this copy. |
| 78 | + destinations.set(node.target, dest) |
| 79 | + } else { |
| 80 | + // The link target is already in the destination |
| 81 | + await relativeSymlink(targetPath, dest) |
| 82 | + } |
| 83 | + } else { |
| 84 | + if (node.isWorkspace || node.isRoot) { |
| 85 | + // workspace and root packages have not been published so they may |
| 86 | + // have files that should be excluded. |
| 87 | + await copyPacklist(node.target.realpath, dest) |
| 88 | + } else { |
| 89 | + // copy the modules files but not dependencies. |
| 90 | + const nm = join(node.realpath, 'node_modules') |
| 91 | + await fs.cp(node.realpath, dest, { |
| 92 | + recursive: true, |
| 93 | + errorOnExist: false, |
| 94 | + filter: src => src !== nm, |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + // add dependency edges to the queue. |
| 99 | + for (const edge of node.edgesOut.values()) { |
| 100 | + if (!omit.has(edge.type) && edge.to != null) { |
| 101 | + destinations.set( |
| 102 | + edge.to, |
| 103 | + join( |
| 104 | + destinations.get(edge.to.parent) || destination, |
| 105 | + relative(edge.to.parent.location, edge.to.location))) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | +module.exports = Copy |
| 113 | + |
| 114 | +async function copyPacklist (from, to) { |
| 115 | + for (const file of await packlist({ path: from })) { |
| 116 | + // packlist will include bundled node_modules. ignore it because we're |
| 117 | + // already handling copying dependencies. |
| 118 | + if (file.startsWith('node_modules/')) { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + // using recursive copy because packlist doesn't list directories. |
| 123 | + // TODO what is npm's preferred recursive copy? |
| 124 | + await fs.cp( |
| 125 | + join(from, file), |
| 126 | + join(to, file), |
| 127 | + { recursive: true, errorOnExist: false }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +async function relativeSymlink (target, path) { |
| 132 | + await fs.mkdir(dirname(path), { recursive: true }) |
| 133 | + await fs.symlink( |
| 134 | + './' + relative(dirname(path), target), |
| 135 | + path // link to create |
| 136 | + ) |
| 137 | +} |
0 commit comments