-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
npm copy
copy module files and dependencies into a deployable folder
#4082
Open
everett1992
wants to merge
1
commit into
npm:latest
Choose a base branch
from
everett1992:copy
base: latest
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+404
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
const Arborist = require('@npmcli/arborist') | ||
const { join, relative, dirname } = require('path') | ||
const packlist = require('npm-packlist') | ||
const fs = require('@npmcli/fs') | ||
|
||
const BaseCommand = require('../base-command.js') | ||
|
||
class Copy extends BaseCommand { | ||
static description = 'Copy package to new location' | ||
|
||
static name = 'copy' | ||
|
||
static params = [ | ||
'omit', | ||
'workspace', | ||
'workspaces', | ||
'include-workspace-root', | ||
] | ||
|
||
static ignoreImplicitWorkspace = false | ||
|
||
static usage = ['<destination>'] | ||
|
||
async exec (args) { | ||
await this.copyTo(args, true, new Set([])) | ||
} | ||
|
||
// called when --workspace or --workspaces is passed. | ||
async execWorkspaces (args, filters) { | ||
await this.setWorkspaces(filters) | ||
|
||
await this.copyTo( | ||
args, | ||
this.includeWorkspaceRoot, | ||
new Set(this.workspacePaths)) | ||
} | ||
|
||
async copyTo (args, includeWorkspaceRoot, workspaces) { | ||
if (args.length !== 1) { | ||
throw this.usageError('Missing required destination argument') | ||
} | ||
const opts = { | ||
...this.npm.flatOptions, | ||
path: this.npm.localPrefix, | ||
log: this.npm.log, | ||
} | ||
const destination = args[0] | ||
const omit = new Set(this.npm.flatOptions.omit) | ||
|
||
const tree = await new Arborist(opts).loadActual() | ||
|
||
// map of node to location in destination. | ||
const destinations = new Map() | ||
|
||
// calculate the root set of packages. | ||
if (includeWorkspaceRoot) { | ||
const to = join(destination, tree.location) | ||
destinations.set(tree, to) | ||
} | ||
for (const edge of tree.edgesOut.values()) { | ||
if (edge.workspace && workspaces.has(edge.to.realpath)) { | ||
const to = join(destination, edge.to.location) | ||
destinations.set(edge.to, to) | ||
} | ||
} | ||
|
||
// copy the root set of packages and their dependencies. | ||
for (const [node, dest] of destinations) { | ||
if (node.isLink && node.target) { | ||
const targetPath = destinations.get(node.target) | ||
if (targetPath == null) { | ||
// This is the first time the link target was seen, it will be the | ||
// only copy in dest, other links to the same target will link to | ||
// this copy. | ||
destinations.set(node.target, dest) | ||
} else { | ||
// The link target is already in the destination | ||
await relativeSymlink(targetPath, dest) | ||
} | ||
} else { | ||
if (node.isWorkspace || node.isRoot) { | ||
// workspace and root packages have not been published so they may | ||
// have files that should be excluded. | ||
await copyPacklist(node.target.realpath, dest) | ||
} else { | ||
// copy the modules files but not dependencies. | ||
const nm = join(node.realpath, 'node_modules') | ||
await fs.cp(node.realpath, dest, { | ||
recursive: true, | ||
errorOnExist: false, | ||
filter: src => src !== nm, | ||
}) | ||
} | ||
|
||
// add dependency edges to the queue. | ||
for (const edge of node.edgesOut.values()) { | ||
if (!omit.has(edge.type) && edge.to != null) { | ||
destinations.set( | ||
edge.to, | ||
join( | ||
destinations.get(edge.to.parent) || destination, | ||
relative(edge.to.parent.location, edge.to.location))) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
module.exports = Copy | ||
|
||
async function copyPacklist (from, to) { | ||
for (const file of await packlist({ path: from })) { | ||
// packlist will include bundled node_modules. ignore it because we're | ||
// already handling copying dependencies. | ||
if (file.startsWith('node_modules/')) { | ||
continue | ||
} | ||
|
||
// using recursive copy because packlist doesn't list directories. | ||
// TODO what is npm's preferred recursive copy? | ||
await fs.cp( | ||
join(from, file), | ||
join(to, file), | ||
{ recursive: true, errorOnExist: false }) | ||
} | ||
} | ||
|
||
async function relativeSymlink (target, path) { | ||
await fs.mkdir(dirname(path), { recursive: true }) | ||
await fs.symlink( | ||
'./' + relative(dirname(path), target), | ||
path // link to create | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mkdir fails on Windows when the directory already exists, even with recursive: true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i've spent more time on this EEXIST issue, and it seems that pushing fs.cp calls inside tasks isn't safe. mkdir operations happen concurrently under the hood multiple times for a same folder, which results in an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can drop parallel tasks and just run sequentially