Skip to content

Commit

Permalink
feat(publish): exclude and include
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Jan 23, 2024
1 parent d1eed98 commit 37dd9e6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 12 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,6 @@ opt-level = 3
opt-level = 3
[profile.release.package.base64-simd]
opt-level = 3

[patch.crates-io]
deno_config = { git = "https://github.com/denoland/deno_config", "rev" = "133702b208996e293736c82884451f7ec5720a93" }
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
32 changes: 32 additions & 0 deletions cli/tests/integration/publish_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,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 @@ -152,6 +156,34 @@ 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",
"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 @@ -130,17 +130,15 @@ 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 tarball = deno_core::unsync::spawn_blocking(move || {
let unfurler = ImportMapUnfurler::new(&import_map);
tar::create_gzipped_tarball(
&dir_path,
&*source_cache,
&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;

#[derive(Debug, Clone, PartialEq)]
pub struct PublishableTarballFile {
Expand All @@ -33,7 +33,7 @@ pub fn create_gzipped_tarball(
dir: &Path,
source_cache: &dyn deno_graph::ParsedSourceStore,
unfurler: &ImportMapUnfurler,
exclude_patterns: &PathOrPatternSet,
file_patterns: Option<FilePatterns>,
) -> Result<PublishableTarball, AnyError> {
let mut tar = TarGzArchive::new();
let mut diagnostics = vec![];
Expand All @@ -43,11 +43,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 37dd9e6

Please sign in to comment.