Skip to content

Commit

Permalink
feat(publish): exclude and include (#22055)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Jan 24, 2024
1 parent fb24b00 commit 176118a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ winres.workspace = true
[dependencies]
deno_ast = { workspace = true, features = ["bundler", "cjs", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] }
deno_cache_dir = "=0.6.1"
deno_config = "=0.8.2"
deno_config = "=0.9.1"
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_doc = { version = "=0.94.1", features = ["html"] }
deno_emit = "=0.34.0"
Expand Down
20 changes: 20 additions & 0 deletions cli/schemas/config-file.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,26 @@
}
}
},
"publish": {
"description": "Configuration for deno publish",
"type": "object",
"properties": {
"include": {
"type": "array",
"description": "List of files, directories or globs that will be included in the published package.",
"items": {
"type": "string"
}
},
"exclude": {
"type": "array",
"description": "List of files, directories or globs that will be excluded from the published package.",
"items": {
"type": "string"
}
}
}
},
"bench": {
"description": "Configuration for deno bench",
"type": "object",
Expand Down
33 changes: 33 additions & 0 deletions cli/tests/integration/publish_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,17 @@ fn ignores_directories() {
"name": "@foo/bar",
"version": "1.0.0",
"exclude": [ "ignore" ],
"publish": {
"exclude": [ "ignore2" ]
},
"exports": "./main_included.ts"
}));

let ignored_dirs = vec![
temp_dir.join(".git"),
temp_dir.join("node_modules"),
temp_dir.join("ignore"),
temp_dir.join("ignore2"),
];
for ignored_dir in ignored_dirs {
ignored_dir.create_dir_all();
Expand All @@ -163,6 +167,35 @@ fn ignores_directories() {
assert_not_contains!(output, "ignored.ts");
}

#[test]
fn includes_directories() {
let context = publish_context_builder().build();
let temp_dir = context.temp_dir().path();
temp_dir.join("deno.json").write_json(&json!({
"name": "@foo/bar",
"version": "1.0.0",
"exports": "./main.ts",
"publish": {
"include": [ "deno.json", "main.ts" ]
}
}));

temp_dir.join("main.ts").write("");
temp_dir.join("ignored.ts").write("");

let output = context
.new_command()
.arg("publish")
.arg("--log-level=debug")
.arg("--token")
.arg("sadfasdf")
.run();
output.assert_exit_code(0);
let output = output.combined_output();
assert_contains!(output, "main.ts");
assert_not_contains!(output, "ignored.ts");
}

fn publish_context_builder() -> TestContextBuilder {
TestContextBuilder::new()
.use_http_server()
Expand Down
6 changes: 2 additions & 4 deletions cli/tools/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ async fn prepare_publish(
let Some((scope, package_name)) = name.split_once('/') else {
bail!("Invalid package name, use '@<scope_name>/<package_name> format");
};
let exclude_patterns = deno_json
.to_files_config()
.map(|files| files.map(|f| f.exclude).unwrap_or_default())?;
let file_patterns = deno_json.to_publish_config()?.map(|c| c.files);

let diagnostics_collector = diagnostics_collector.clone();
let tarball = deno_core::unsync::spawn_blocking(move || {
Expand All @@ -143,7 +141,7 @@ async fn prepare_publish(
&*source_cache,
&diagnostics_collector,
&unfurler,
&exclude_patterns,
file_patterns,
)
.context("Failed to create a tarball")
})
Expand Down
14 changes: 8 additions & 6 deletions cli/tools/registry/tar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use bytes::Bytes;
use deno_config::glob::FilePatterns;
use deno_core::anyhow;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
Expand All @@ -13,7 +14,6 @@ use std::path::PathBuf;
use tar::Header;

use crate::util::import_map::ImportMapUnfurler;
use deno_config::glob::PathOrPatternSet;

use super::diagnostics::PublishDiagnostic;
use super::diagnostics::PublishDiagnosticsCollector;
Expand All @@ -37,7 +37,7 @@ pub fn create_gzipped_tarball(
source_cache: &dyn deno_graph::ParsedSourceStore,
diagnostics_collector: &PublishDiagnosticsCollector,
unfurler: &ImportMapUnfurler,
exclude_patterns: &PathOrPatternSet,
file_patterns: Option<FilePatterns>,
) -> Result<PublishableTarball, AnyError> {
let mut tar = TarGzArchive::new();
let mut diagnostics = vec![];
Expand All @@ -47,11 +47,13 @@ pub fn create_gzipped_tarball(
while let Some(entry) = iterator.next() {
let entry = entry?;

if exclude_patterns.matches_path(entry.path()) {
if entry.file_type().is_dir() {
iterator.skip_current_dir();
if let Some(file_patterns) = &file_patterns {
if !file_patterns.matches_path(entry.path()) {
if entry.file_type().is_dir() {
iterator.skip_current_dir();
}
continue;
}
continue;
}

if entry.file_type().is_file() {
Expand Down

0 comments on commit 176118a

Please sign in to comment.