Skip to content

Commit cc271c4

Browse files
committed
feat&perf: add temporary directory parameter, all files generated by the plugin will be moved to the temporary directory
1 parent c4b13ca commit cc271c4

8 files changed

Lines changed: 159 additions & 64 deletions

File tree

dash_tailwindcss_plugin/cli.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,24 @@ def run(self):
3838
action='append',
3939
help='Glob pattern for files to scan for Tailwind classes. Can be specified multiple times.',
4040
)
41+
parser.add_argument(
42+
'--plugin-tmp-dir',
43+
default='./_tailwind',
44+
help='Path to temporary directory for plugin files',
45+
)
4146
parser.add_argument(
4247
'--input-css-path',
43-
default='./.tailwind/tailwind_input.css',
48+
default='./_tailwind/tailwind_input.css',
4449
help='Path to input CSS file',
4550
)
4651
parser.add_argument(
4752
'--output-css-path',
48-
default='./.tailwind/tailwind.css',
53+
default='./_tailwind/tailwind.css',
4954
help='Path to output CSS file',
5055
)
5156
parser.add_argument(
5257
'--config-js-path',
53-
default='./.tailwind/tailwind.config.js',
58+
default='./_tailwind/tailwind.config.js',
5459
help='Path to Tailwind config file',
5560
)
5661
parser.add_argument(
@@ -97,6 +102,7 @@ def run(self):
97102
npx_path=node_manager.npx_path,
98103
tailwind_version=args.tailwind_version,
99104
content_path=args.content_path if args.content_path else ['**/*.py'],
105+
plugin_tmp_dir=args.plugin_tmp_dir,
100106
input_css_path=args.input_css_path,
101107
output_css_path=args.output_css_path,
102108
config_js_path=args.config_js_path,

dash_tailwindcss_plugin/plugin.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ def __init__(
1515
mode: Literal['offline', 'online'] = 'offline',
1616
tailwind_version: Literal['3', '4'] = '3',
1717
content_path: List[str] = ['**/*.py'],
18-
input_css_path: str = '.tailwind/tailwind_input.css',
19-
output_css_path: str = '.tailwind/tailwind.css',
20-
config_js_path: str = '.tailwind/tailwind.config.js',
18+
plugin_tmp_dir: str = '_tailwind',
19+
input_css_path: str = '_tailwind/tailwind_input.css',
20+
output_css_path: str = '_tailwind/tailwind.css',
21+
config_js_path: str = '_tailwind/tailwind.config.js',
2122
cdn_url: str = 'https://cdn.tailwindcss.com',
2223
download_node: bool = False,
2324
node_version: str = '18.17.0',
@@ -33,6 +34,7 @@ def __init__(
3334
mode (Literal['offline', 'online']): Mode of operation ('offline' or 'online')
3435
tailwind_version (Literal['3', '4']): Version of Tailwind CSS
3536
content_path (List[str]): Glob patterns for files to scan for Tailwind classes
37+
plugin_tmp_dir (str): Temporary directory for plugin files
3638
input_css_path (str): Path to input CSS file
3739
output_css_path (str): Path to output CSS file
3840
config_js_path (str): Path to Tailwind config file
@@ -57,6 +59,7 @@ def __init__(
5759
npx_path=node_manager.npx_path,
5860
tailwind_version=tailwind_version,
5961
content_path=content_path,
62+
plugin_tmp_dir=plugin_tmp_dir,
6063
input_css_path=input_css_path,
6164
output_css_path=output_css_path,
6265
config_js_path=config_js_path,
@@ -66,6 +69,7 @@ def __init__(
6669
self.mode = mode
6770
self.tailwind_version = tailwind_version
6871
self.content_path = content_path
72+
self.plugin_tmp_dir = plugin_tmp_dir
6973
self.input_css_path = input_css_path
7074
self.output_css_path = output_css_path
7175
self.config_js_path = config_js_path
@@ -204,9 +208,10 @@ def setup_tailwindcss_plugin(
204208
mode: Literal['online', 'offline'] = 'offline',
205209
tailwind_version: Literal['3', '4'] = '3',
206210
content_path: List[str] = ['**/*.py'],
207-
input_css_path: str = '.tailwind/tailwind_input.css',
208-
output_css_path: str = '.tailwind/tailwind.css',
209-
config_js_path: str = '.tailwind/tailwind.config.js',
211+
plugin_tmp_dir: str = '_tailwind',
212+
input_css_path: str = '_tailwind/tailwind_input.css',
213+
output_css_path: str = '_tailwind/tailwind.css',
214+
config_js_path: str = '_tailwind/tailwind.config.js',
210215
cdn_url: str = 'https://cdn.tailwindcss.com',
211216
download_node: bool = False,
212217
node_version: str = '18.17.0',
@@ -222,6 +227,7 @@ def setup_tailwindcss_plugin(
222227
mode (Literal['online', 'offline']): Mode of operation ('offline' or 'online')
223228
tailwind_version (Literal['3', '4']): Version of Tailwind CSS
224229
content_path (List[str]): Glob patterns for files to scan for Tailwind classes
230+
plugin_tmp_dir (str): Temporary directory for plugin files
225231
input_css_path (str): Path to input CSS file
226232
output_css_path (str): Path to output CSS file
227233
config_js_path (str): Path to Tailwind config file
@@ -237,6 +243,7 @@ def setup_tailwindcss_plugin(
237243
mode=mode,
238244
tailwind_version=tailwind_version,
239245
content_path=content_path,
246+
plugin_tmp_dir=plugin_tmp_dir,
240247
input_css_path=input_css_path,
241248
output_css_path=output_css_path,
242249
config_js_path=config_js_path,

dash_tailwindcss_plugin/utils.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def __init__(
337337
npx_path: str,
338338
tailwind_version: Literal['3', '4'],
339339
content_path: List[str],
340+
plugin_tmp_dir: str,
340341
input_css_path: str,
341342
output_css_path: str,
342343
config_js_path: str,
@@ -370,6 +371,10 @@ def __init__(
370371
self.config_js_path = config_js_path
371372
self.is_cli = is_cli
372373
self.theme_config = theme_config or {}
374+
# Ensure the tailwind_plugin directory exists
375+
self.plugin_tmp_dir = plugin_tmp_dir
376+
if not os.path.exists(self.plugin_tmp_dir):
377+
os.makedirs(self.plugin_tmp_dir)
373378

374379
def create_default_input_tailwindcss(self):
375380
"""
@@ -463,7 +468,7 @@ def _check_npm_init(self) -> bool:
463468
Returns:
464469
bool: True if npm init has been run, False otherwise
465470
"""
466-
return os.path.exists('package.json')
471+
return os.path.exists(f'{self.plugin_tmp_dir}/package.json')
467472

468473
def _check_tailwindcss(self) -> bool:
469474
"""
@@ -474,7 +479,7 @@ def _check_tailwindcss(self) -> bool:
474479
"""
475480
check_cmd = [self.npx_path, f'{self._tailwind_cli} --help']
476481

477-
result = subprocess.run(check_cmd, capture_output=True, text=True, env=self.node_env)
482+
result = subprocess.run(check_cmd, capture_output=True, text=True, cwd=self.plugin_tmp_dir, env=self.node_env)
478483
return result.returncode == 0
479484

480485
def init(self) -> Self:
@@ -516,7 +521,9 @@ def init(self) -> Self:
516521

517522
if not self._check_npm_init():
518523
init_cmd = [self.npm_path, 'init', '-y']
519-
result = subprocess.run(init_cmd, capture_output=True, text=True, env=self.node_env)
524+
result = subprocess.run(
525+
init_cmd, capture_output=True, text=True, cwd=self.plugin_tmp_dir, env=self.node_env
526+
)
520527
if result.returncode != 0:
521528
raise RuntimeError(result.stderr)
522529

@@ -544,7 +551,9 @@ def install(self) -> Self:
544551
'-D',
545552
*self._tailwind_package,
546553
]
547-
result = subprocess.run(install_cmd, capture_output=True, text=True, env=self.node_env)
554+
result = subprocess.run(
555+
install_cmd, capture_output=True, text=True, cwd=self.plugin_tmp_dir, env=self.node_env
556+
)
548557
if result.returncode != 0:
549558
raise RuntimeError(result.stderr)
550559

@@ -567,7 +576,7 @@ def build(self) -> Self:
567576
try:
568577
build_cmd: list[str] = [
569578
self.npx_path,
570-
self._tailwind_cli,
579+
f'{self.plugin_tmp_dir}/node_modules/{self._tailwind_cli}',
571580
'-i',
572581
self.input_css_path,
573582
'-o',
@@ -601,7 +610,7 @@ def watch(self) -> Self:
601610
try:
602611
watch_cmd = [
603612
self.npx_path,
604-
self._tailwind_cli,
613+
f'{self.plugin_tmp_dir}/node_modules/{self._tailwind_cli}',
605614
'-i',
606615
self.input_css_path,
607616
'-o',
@@ -632,12 +641,12 @@ def clean(self) -> Self:
632641
try:
633642
files_to_remove = [
634643
self.config_js_path,
635-
'package.json',
636-
'package-lock.json',
644+
f'{self.plugin_tmp_dir}/package.json',
645+
f'{self.plugin_tmp_dir}/package-lock.json',
637646
self.input_css_path,
638647
]
639648

640-
directories_to_remove = ['node_modules']
649+
directories_to_remove = [f'{self.plugin_tmp_dir}/node_modules']
641650

642651
# Remove files
643652
for file_path in files_to_remove:

0 commit comments

Comments
 (0)